WordPress小部件中未保存的复选框数组

时间:2013-12-29 05:32:37

标签: wordpress wordpress-plugin

这是我的小部件代码:

 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;

    }

这是视图的代码:

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

小部件按预期显示复选框,但状态不会保存。在update函数中转储$ old_instance变量会给出null值。

3 个答案:

答案 0 :(得分:2)

在视图文件中,您正确定义了输入,但与name属性不同。 像这样定义name属性:

           <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']; ?>

答案 1 :(得分:0)

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

此函数必须与默认构造函数一起添加。否则小部件将无效。

答案 2 :(得分:0)

不完全是你想要的,但这是我在metabox中保存一个复选框的方式,你可能会从中得到一些暗示......

用于显示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;
}

保存时

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

为管理界面添加了js

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