WordPress.org

Ready to get started?Download WordPress

Forums

How can i save a WordPress checkbox array in a widget? (2 posts)

  1. wedideas
    Member
    Posted 2 months ago #

    I'm writing a plugin/widget that will enable a user to pick some specific posts from a list of checkboxes returned from a WP get_posts query.

    I've written the following code but for some reason I can't get the checkbox values to stick - the options are either all on or all off. If I select one or more of the checkboxes, they all end up checked - I presume because they aren't being written as an array but have no idea how I can update the separate values?

    <?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
    */
    
    // register widget
    add_action('widgets_init',
         create_function('', 'return register_widget("HotTopics");')
    );
    
    class HotTopics extends WP_Widget {
    
    ///////////////////////////
    // Initialise the widget //
    /////////////////////////// 
    
        function HotTopics() {
            $this->WP_Widget(
                'hottopics',
                __('Hot Topics'),
                array(
                    'name' => 'Hot Topics',
                    /*'classname' => 'widget-hot-topics',*/
                    'description' => __( "Use this widget to choose an array of posts snippets to show in the sidebar." )
                )
            );
        }
    
        /**
         * Outputs the content of the widget
         *
         * @param array $args
         * @param array $instance
         */
        public function widget( $args, $instance ) {
            // outputs the content of the widget
        }
    
        /**
         * Ouputs the options form on admin
         *
         * @param array $instance The widget options
         */
        public function form ($instance) {
    
          $instance = wp_parse_args( (array) $instance, $defaults ); 
    
          // Populate the select list with the post titles
            $get_posts = array('1'=>'First option','2'=>'Second option','3'=>'Third option','4'=>'Fourth option','5'=>'Fifth option',);
    
            foreach( $get_posts as $post ) { ?>
    
            <p><input type="checkbox" id="<?php echo $this->get_field_id('rss'); ?>" name="<?php echo $this->get_field_name('rss'); ?>[]" <?php if ($instance['rss']) echo 'checked="checked"' ?> />
           <label for="<?php echo $this->get_field_id('rss'); ?>"><?php echo $post; ?></label></p>
    
            <?php }
    
        }
    
        /**
         * Processing widget options on save
         *
         * @param array $new_instance The new options
         * @param array $old_instance The previous options
         */
        public function update ($new_instance, $old_instance) {
          $instance = $old_instance;
          $instance['rss'] = $new_instance['rss'];
    
          return $instance;
        }
    }
    ?>
  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 months ago #

    Try reviewing the Settings_API.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.