php - Wordpress 自定义小部件记住多个选择选项

标签 php widget multipleselection wordpress

我正在为我们的网站编写一个自定义小部件来显示一些选定的帖子。在管理部分,我有一个多选框让管理员通过他们的名字选择多个帖子。这工作正常,但是当我选择几个不同的帖子并保存时,没有任何内容被保存。

任何人都可以阐明这一点吗?

这是我的代码……

<?php
/* 
Plugin Name: Hot Topics
Plugin URI: http://www.weddingideasmag.com
Description: Use this widget to choose an array of posts snippets to show
Version: 1.0)
Author: James Payne
Author URI: http://www.bluntcreative.co.uk
License: GPL2
*/


class HotTopics extends WP_Widget {

// constructor
function HotTopics() {
    $widget_ops = array( 'name' => 'Hot Topics','classname' => 'widget-hot-topics', 'description' => __( "Use this widget to choose an array of posts snippets to show in the sidebar." ) );
    $this->WP_Widget( 'hottopics', __('Hot Topics'), $widget_ops);
}

// widget form creation
function form($instance) {  
    // Check values
    if( $instance) {
        $select = esc_attr($instance['select']); // Added 
    } else {
         $select ='';
    }
    ?>

    <select multiple="multiple" name="<?php echo $this->get_field_name('select'); ?>[]" id="<?php echo $this->get_field_id('select'); ?>" class="widefat" size="15" style="margin-bottom:15px;">
        <?php
        $args = array( 'offset'=> 1, 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 200, 'post_status' => 'publish' );

        // The Query
        query_posts( $args );

        // The Loop
        while ( have_posts() ) : the_post();

        $title = get_the_title();
        ?>
            <option value="<?php echo get_the_ID();?>" class="hot-topic" <?php $select == $title ? ' selected="selected"' : '';?> style="margin-bottom:3px;">
                <?php echo $title;?>
            </option>
            <?php
        endwhile;

        // Reset Query
        wp_reset_query();
        ?>
    </select>

    <?php
}

// update widget
function update($new_instance, $old_instance) {
      $instance = $old_instance;
      // Fields
      $instance['select'] = strip_tags($new_instance['select']);
     return $instance;
}

// widget display
function widget($args, $instance) {
    /* ... */
    echo 'tetst';
}
}

// register widget
add_action('widgets_init', create_function('', 'return register_widget("HotTopics");'));

?>

最佳答案

更新小部件时,strip_tags正在破坏选定帖子的数组。 esc_sql完成工作。另外,don't use query_posts .最后,存储帖子标题并不理想,因为它可能会更改,ID 是永久的。

工作示例:

# PHP 5.3+ anonymous function
add_action( 'widgets_init', function() {
    register_widget( 'Sample_Widget_SO_19246434' );
});

class Sample_Widget_SO_19246434 extends WP_Widget 
{
    function Sample_Widget_SO_19246434() 
    {
        $this->WP_Widget( 
            'hottopics', 
            __('Hot Topics'),
            array( 
                'name' => 'Hot Topics',
                'classname' => 'widget-hot-topics', 
                'description' => __( "Description" ) 
            )
        );
    }

    function form( $instance ) 
    {
        if( $instance ) 
            $select = $instance['select'];
        else
             $select ='';

        $get_posts = get_posts( array( 
            'offset'=> 1, 
            'orderby' => 'date', 
            'order' => 'DESC', 
            'posts_per_page' => 200, 
            'post_status' => 'publish' 
        ));
        if( $get_posts )
        {
            printf(
                '<select multiple="multiple" name="%s[]" id="%s" class="widefat" size="15">',
                $this->get_field_name('select'),
                $this->get_field_id('select')
            );
            foreach( $get_posts as $post )
            {
                printf(
                    '<option value="%s" class="hot-topic" %s style="margin-bottom:3px;">%s</option>',
                    $post->ID,
                    in_array( $post->ID, $select) ? 'selected="selected"' : '',
                    $post->post_title
                );
            }
            echo '</select>';
        }
        else
            echo 'No posts found :(';
    }

    function update( $new_instance, $old_instance ) 
    {
        $instance = $old_instance;
        $instance['select'] = esc_sql( $new_instance['select'] );
        return $instance;
    }

    function widget( $args, $instance ) 
    {
        echo 'Hello world';
    }
}

相关:
<子> • What's the difference between esc_* functions?
<子> • How to sanitize user input?

关于php - Wordpress 自定义小部件记住多个选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19246434/

相关文章:

php - 在显示进度的同时使用 IMAP 阅读电子邮件

javascript - 使用filter.js的多个id选择器

选择多行进行排序时发生 JavaFX TreeTableView 异常

javascript - 如何在拖放 JavaScript 中控制源 TD 样式?

php - 通过 PDO 将数据插入 MySQL,尝试使用数组将时间戳与数据一起包含 - 出现错误?

reactjs - Material UI 根据元素大小进行响应

widget - IPython/Jupyter 对齐小部件

android - 当我点击它时,是否可以使 android 应用程序启动器图标动画化?

vba - 使用多个不连续选择

javascript - jQuery/javascript - 根据下拉选择更改图像