• 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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter cwz001

    (@cwz001)

    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;
                      }

    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. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Widget Checkbox Group – not registering.’ is closed to new replies.