未保存在WordPress小部件中的复选框数组
这是我的小部件代码:未保存在WordPress小部件中的复选框数组,wordpress,Wordpress,这是我的小部件代码: function form( $instance ) { $instance = wp_parse_args( (array)$instance, array( 'checkboxes' => array( 'Monday' => array('name' => 'Monday', 'value' => 'Monday', 'checked' =>
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>
此函数必须与默认构造函数一起添加。否则,该小部件将无法工作。在视图文件中,您正确定义了输入的id,但在“名称”属性中没有定义相同的id。
定义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']; ?>
不完全是您想要的,但这就是我在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;
}
添加了用于管理界面的js
function checkbox_helper(){
var ele =jQuery('#_suppress_rtsocial'),value;
ele.click(function(){
value = ele.is(':checked')?'yes':'no';
ele.val(value);
}
);
}
是的,我有,我只是没有把它放在我的代码里。正如我所说,表单显示正确,但是状态不会保存,如果我在更新函数中转储$new_instance变量,我会得到一个空数组;您实际生成的HTML是什么?有关小部件对象内的复选框数组主题,请参阅Checkout my Full functional code sample中的问题
update_post_meta($post_id,'_suppress_rtsocial',$_POST['_suppress_rtsocial']);
function checkbox_helper(){
var ele =jQuery('#_suppress_rtsocial'),value;
ele.click(function(){
value = ele.is(':checked')?'yes':'no';
ele.val(value);
}
);
}