pelachile
pelachile

Reputation: 587

Array of checkboxes not saving in WordPress widget

This is my code for the widget:

 function form( $instance ) {

            $instance = wp_parse_args( (array)$instance, array(
                'checkboxes' => array(
                    'Monday' => array('name' => 'Monday', 'value' => 'Monday', 'checked' => 1),
                    'Tuesday' => array('name' => 'Tuesday', 'value' => 'Tuesday', 'checked' => ''),
                    'Wednesday' => array('name' => 'Wednesday', 'value' => 'Wednesday', 'checked' => ''),
                    'Thursday' => array('name' => 'Thursday', 'value' => 'Thursday', 'checked' => ''),
                    'Friday' => array('name' => 'Friday', 'value' => 'Friday', 'checked' => ''),
                    'Saturday' => array('name' => 'Saturday', 'value' => 'Saturday', 'checked' => ''),
                    'Sunday' => array('name' => 'Sunday', 'value' => 'Sunday', 'checked' => '')
                ),
                'title' => 'Workdays'
            ));

        include( plugin_dir_path(__FILE__) . '/views/admin.php' );
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['checkboxes'] = strip_tags($new_instance['checkboxes']);

        return $instance;

    }

This is the code for the view:

<div class='ws-business-info'>
<div class='form-group'>
    <?php foreach($instance['checkboxes'] as $day ) : ?>
        <div class='checkbox'>
            <label>
                <input type="checkbox"
                       name="<?php echo $day['name']; ?>"
                      class="form-control"
                         id="<?php echo $this->get_field_id($day['name']); ?>"
                      value="<?php echo $day['value']; ?>"
                   <?php checked('1', $day['checked']); ?>/>
                   <?php echo $day['name']; ?>
            </label>
        </div>
    <?php endforeach; ?>
</div>

The widget displays the checkboxes as expected but the state will not save. Dumping the $old_instance variable in the update function gives a null value.

Upvotes: 1

Views: 2164

Answers (3)

Boris Belenski
Boris Belenski

Reputation: 1412

In the view file you defined input's id corectly, but you did not the same with name attribute. Define the name attribute like this:

           <input type="checkbox"
               name="<?php echo $this->get_field_name( 'checkboxes' ), '[', esc_attr( $day['name'] ), ']'; ?>"
              class="form-control"
                 id="<?php echo $this->get_field_id($day['name']); ?>"
              value="<?php echo $day['value']; ?>"
           <?php checked('1', $day['checked']); ?>/>
           <?php echo $day['name']; ?>

Upvotes: 2

Yash
Yash

Reputation: 919

Not exactly what you are looking for but this is how I saved a checkbox in metabox, you may get some hint from this...

Code used to display html

function html_rtsocial_metabox()
{
  global $post;

  $custom = get_post_custom($post->ID);
  $suppress = $custom['_suppress_rtsocial'][0];
  $checked = ($suppress=='yes')?'checked':'';
  $value   = ($suppress=='yes')?'yes':'no';
  $html  = '<br><input type="checkbox" name="_suppress_rtsocial" value="'.$value.'" '.$checked.' id="_suppress_rtsocial" /> Hide Social Icons ???';
  $html .= '<br/><br/>';
  echo $html;
}

While saving

update_post_meta($post_id,'_suppress_rtsocial',$_POST['_suppress_rtsocial']);

Added js for admin interface

function checkbox_helper(){
  var ele =jQuery('#_suppress_rtsocial'),value;
  ele.click(function(){
              value = ele.is(':checked')?'yes':'no';
              ele.val(value);
            }
);
}

Upvotes: 0

Harish Kotra
Harish Kotra

Reputation: 169

public function widget( $args, $instance ) {
        // outputs the content of the widget
    }

This function has to be added along with the default constructor. Else the widget will not work.

Upvotes: 0

Related Questions