WordPress.org

Ready to get started?Download WordPress

Forums

Widget Checkbox Group - not registering. (3 posts)

  1. cwz001
    Member
    Posted 4 years ago #

    Hi,
    I'm having difficulties getting my checkboxes to save in a widget I'm developing. I'm pulling out the categories and displaying them as checkboxes, but I want them as a group as opposed to individual.

    Saving checkbox group value using below -
    $instance['gallerycats[]'] = isset($new_instance['gallerycats[]']);

    Displaying the checkboxes as follows -

    $categories=  get_categories();
    foreach ($categories as $cat) {
    
    $option='<input type="checkbox" id="'. $this->get_field_id( 'gallerycats[]' ) .'" name="'. $this->get_field_name( 'gallerycats[]' ) .'"';
    					if (is_array($instance['gallerycats[]'])) {
    					foreach ($instance['gallerycats[]'] as $cats) {
    					if($cats==$cat->term_id) {
    					$option=$option.' checked="checked"';
    					}
    					}
    					}
    					$option .= ' value="'.$cat->term_id.'" />';
    
                        $option .= $cat->cat_name;
                        $option .= ' ('.$cat->category_count.')';
                        $option .= '<br />';
                        echo $option;
                      }

    Thanks in advance!

  2. cwz001
    Member
    Posted 4 years ago #

    Sorted.

    Place the [] outside of the variable.

    $instance['gallerycats'] = $new_instance['gallerycats'];

    $categories=  get_categories();
    foreach ($categories as $cat) {
    	$option='<input type="checkbox" id="'. $this->get_field_id( 'gallerycats' ) .'[]" name="'. $this->get_field_name( 'gallerycats' ) .'[]"';
    					if (is_array($instance['gallerycats'])) {
    					foreach ($instance['gallerycats'] as $cats) {
    					if($cats==$cat->term_id) {
    					$option=$option.' checked="checked"';
    					}
    					}
    					}
    					$option .= ' value="'.$cat->term_id.'" />';
    
                        $option .= $cat->cat_name;
                        $option .= ' ('.$cat->category_count.')';
                        $option .= '<br />';
                        echo $option;
                      }
  3. DavidM
    Member
    Posted 3 years ago #

    Thanks for posting your own solution. It helps those of us who are doing the same thing, yet it's something many people leave unattended after they solve. :)

Topic Closed

This topic has been closed to new replies.

About this Topic