【问题标题】:Array of checkboxes not saving in WordPress widget复选框数组未保存在 WordPress 小部件中
【发布时间】:2014-01-05 02:55:54
【问题描述】:

这是我的小部件代码:

 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>

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

【问题讨论】:

标签: wordpress


【解决方案1】:

在视图文件中,您正确定义了输入的 id,但您没有与 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']; ?>

【讨论】:

    【解决方案2】:
    public function widget( $args, $instance ) {
            // outputs the content of the widget
        }
    

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

    【讨论】:

    • 是的,我有那个,我只是没有把它放在我的代码中。正如我所说,表单显示正确,但状态不会保存,如果我在更新函数中转储 $new_instance 变量,我会得到一个空数组。
    【解决方案3】:

    不完全是您要查找的内容,但这是我在元框中保存复选框的方式,您可能会从中得到一些提示...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2012-05-29
      相关资源
      最近更新 更多