【如何在WordPress自定义meta框中保存复选框状态()】我已经编写了以下代码, 但不会保存状态。该复选框始终恢复为选中状态:
<
?php/*META BOXES*/
function add_film_meta_boxes() {
add_meta_box('film_meta_data', 'Film info:', 'film_meta_box_callback', 'film', 'advanced', 'high');
}function film_meta_box_callback( $post ) {
wp_nonce_field('save_film_meta_data', 'spanish_meta_box_nonce');
$spanish = get_post_meta( $post->
ID, '_spanish_value_key', true);
echo '<
label>
Subtitles: <
/label>
';
$spanish_subtitle_field = get_post_meta($post->
ID, 'spanish_subtitle_field', true);
if($spanish_subtitle_field == "yes") {$spanish_subtitle_checked = 'checked="checked"';
} else {$spanish_subtitle_checked = '';
}echo '<
label>
<
input type="checkbox" id="spanish_subtitle_field" name="spanish_subtitle_field" value="http://www.srcmini.com/yes" '.$spanish_subtitle_checked.' />
<
/label>
<
label for="spanish_subtitle_field" style="font-weight:normal !important;
">
spanish &
nbsp;
<
/label>
';
}function save_film_meta_data ($post_id) {
if ( defined('DOING_AUTOSAVE') &
&
DOING_AUTOSAVE )
return;
if ( ! current_user_can( 'edit_post', $post_id ))
return;
if( ! isset($_POST['spanish_meta_box_nonce']) )
return;
if ( ! wp_verify_nonce($_POST['spanish_meta_box_nonce'], 'save_film_meta_data') )
return;
if ( ! isset( $_POST['spanish_subtitle_field']))
return;
$spanish = isset($_POST['spanish_subtitle_field']) ? 'on' : 'off';
update_post_meta($post_id, '_spanish_value_key', $spanish);
}
?>
该代码包含在我的functions.php文件中。
#1这行是错误的, 这会使你的metabox的值为” on” 或” off” 。
$spanish = isset($_POST['spanish_subtitle_field']) ? 'on' : 'off';
当你检查是否被选中时, 你正在寻找一个” 是” 值。
if($spanish_subtitle_field == "yes") {$spanish_subtitle_checked = 'checked="checked"';
} else {$spanish_subtitle_checked = '';
}
因此, 请尝试此新功能并检查是否有效:
function save_film_meta_data($post_id){
if(! isset($_POST['spanish_meta_box_nonce'])){
return;
}
if(!wp_verify_nonce($_POST['spanish_meta_box_nonce'], 'save_film_meta_data')){
return;
}
if( defined('DOING_AUTOSAVE') &
&
DOING_AUTOSAVE){
return;
}
if(! current_user_can('edit_post', $post_id)){
return;
}
if( ! isset($_POST['spanish_subtitle_field'])){
return;
}
$my_data = http://www.srcmini.com/isset($_POST['spanish_subtitle_field']) ? $_POST['spanish_subtitle_field'] : 'no';
update_post_meta($post_id, '_spanish_value_key', $my_data);
}
推荐阅读
- 如何恢复wordpress备份()
- 英国脱欧后,亚马逊电商产品要从新认证
- Tomcat 离线安装
- Docker 安装 Gitlab
- 博睿数据赋能数字化转型,用户体验升级需要有“温度”的技术
- 通过http和浏览器访问Samba共享文件夹
- 测试MD文档
- oeasy教您玩转vim - 45 - # 按行编辑
- 0008 - MapReduce中Shuffle和排序机制解析