Wordpress:关于 update_post_meta 复选框元数据框的问题
Wordpress: issue on update_post_meta for checkbox metabox
我有一个 metabox(一个复选框),在 post 秒内,检查 post 是否会成为特色..
我的代码:
function add_featured_post_checkbox() {
add_meta_box(
'custom_featured_meta',
'Featured post for Sidebar',
'featured_post_checkbox_callback',
'Post',
'side',
'high'
);
} add_action( 'add_meta_boxes', 'add_featured_post_checkbox' );
回调函数:
function featured_post_checkbox_callback( $post ) {
wp_nonce_field( 'custom_save_data' , 'custom_featured_nonce' );
$featured = get_post_meta($post->ID, '_featured_post', true);
echo "<label for='_featured_post'>".__('Is Featured? ', 'foobar')."</label>";
echo "<input type='checkbox' name='_featured_post' id='featured_post' value='1' " . checked(1, $featured) . " />";
}
保存功能:
function custom_save_data( $post_id ) {
if( ! isset( $_POST['custom_featured_nonce'] ) ){
return;
}
if( ! wp_verify_nonce( $_POST['custom_featured_nonce'], 'custom_save_data') ) {
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if( ! isset( $_POST['_featured_post'] ) ) {
return;
}
$my_data_featured = sanitize_text_field( $_POST['_featured_post'] );
update_post_meta( $post_id, '_featured_post', $my_data_featured );
} add_action( 'save_post', 'custom_save_data' );
此代码仅在我想制作 post 精选时才有效..
例如,如果 post 未被选中,而我 select 选择则数据库更新完美(post_meta 中的值为 1)并且 ckeckbox 已勾选盒子
但是之后,如果我尝试取消选中并保存 post,那么 ckeckbox 再次带有勾号并且数据库中没有任何变化..
我试图在 Whosebug 和网络上找到解决方案好几天了,但我找不到。
请帮帮我
谢谢
发生这种情况是因为 return 在检查 _featured_post
是否设置在 $_POST
数组之后。当您不选中该复选框时,它不会被设置。您的方法应该是下一个
if (isset($_POST['_featured_post']))
{
update_post_meta($object_id, '_featured_post', 1); // can only be 1 when checked anyway
}
else
{
delete_post_meta($object_id, '_featured_post');
}
取消选中复选框时,$_POST
将不包含键 _featured_post
。因此,在您的保存回调中,您需要更改您的策略。如果未设置密钥,您不想退出。相反,您想要保存 0 或删除 post 元。让我们选择后面的选项。
function custom_save_data( $post_id ) {
if( ! isset( $_POST['custom_featured_nonce'] ) ){
return;
}
if( ! wp_verify_nonce( $_POST['custom_featured_nonce'], 'custom_save_data') ) {
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['_featured_post'] ) ) {
update_post_meta( $post_id, '_featured_post', 1 );
} else {
delete_post_meta( $post_id, '_featured_post' );
}
}
请注意,您不需要使用 sanitize_text_field()
。为什么?因为这个值只会是一个1
。而已。在这里我们可以对该值进行硬编码。
我看到的另一个问题
运行 你的代码我看到在检查时,checked=checked
的 HTML 被呈现为文本。为什么?因为 运行 函数 checked()
会将其回显给浏览器,除非您告诉它不要这样做。您必须检查您的代码才能:
checked(1, $featured, false);
我有一个 metabox(一个复选框),在 post 秒内,检查 post 是否会成为特色..
我的代码:
function add_featured_post_checkbox() {
add_meta_box(
'custom_featured_meta',
'Featured post for Sidebar',
'featured_post_checkbox_callback',
'Post',
'side',
'high'
);
} add_action( 'add_meta_boxes', 'add_featured_post_checkbox' );
回调函数:
function featured_post_checkbox_callback( $post ) {
wp_nonce_field( 'custom_save_data' , 'custom_featured_nonce' );
$featured = get_post_meta($post->ID, '_featured_post', true);
echo "<label for='_featured_post'>".__('Is Featured? ', 'foobar')."</label>";
echo "<input type='checkbox' name='_featured_post' id='featured_post' value='1' " . checked(1, $featured) . " />";
}
保存功能:
function custom_save_data( $post_id ) {
if( ! isset( $_POST['custom_featured_nonce'] ) ){
return;
}
if( ! wp_verify_nonce( $_POST['custom_featured_nonce'], 'custom_save_data') ) {
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if( ! isset( $_POST['_featured_post'] ) ) {
return;
}
$my_data_featured = sanitize_text_field( $_POST['_featured_post'] );
update_post_meta( $post_id, '_featured_post', $my_data_featured );
} add_action( 'save_post', 'custom_save_data' );
此代码仅在我想制作 post 精选时才有效..
例如,如果 post 未被选中,而我 select 选择则数据库更新完美(post_meta 中的值为 1)并且 ckeckbox 已勾选盒子
但是之后,如果我尝试取消选中并保存 post,那么 ckeckbox 再次带有勾号并且数据库中没有任何变化..
我试图在 Whosebug 和网络上找到解决方案好几天了,但我找不到。
请帮帮我
谢谢
发生这种情况是因为 return 在检查 _featured_post
是否设置在 $_POST
数组之后。当您不选中该复选框时,它不会被设置。您的方法应该是下一个
if (isset($_POST['_featured_post']))
{
update_post_meta($object_id, '_featured_post', 1); // can only be 1 when checked anyway
}
else
{
delete_post_meta($object_id, '_featured_post');
}
取消选中复选框时,$_POST
将不包含键 _featured_post
。因此,在您的保存回调中,您需要更改您的策略。如果未设置密钥,您不想退出。相反,您想要保存 0 或删除 post 元。让我们选择后面的选项。
function custom_save_data( $post_id ) {
if( ! isset( $_POST['custom_featured_nonce'] ) ){
return;
}
if( ! wp_verify_nonce( $_POST['custom_featured_nonce'], 'custom_save_data') ) {
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['_featured_post'] ) ) {
update_post_meta( $post_id, '_featured_post', 1 );
} else {
delete_post_meta( $post_id, '_featured_post' );
}
}
请注意,您不需要使用 sanitize_text_field()
。为什么?因为这个值只会是一个1
。而已。在这里我们可以对该值进行硬编码。
我看到的另一个问题
运行 你的代码我看到在检查时,checked=checked
的 HTML 被呈现为文本。为什么?因为 运行 函数 checked()
会将其回显给浏览器,除非您告诉它不要这样做。您必须检查您的代码才能:
checked(1, $featured, false);