我创建了一个简单的自定义帖子类型,其中有几个文本框和一个复选框。文本框值在中得到了完美的保存和更新wp_postmeta
表,但我无法保存复选框值。
下面是我的代码:
function clients_custom_metaboxes() {
// StreetAddress
add_meta_box(
\'clients_street\',
__( \'Street Address\' ),
\'infotravel_clients_street_metabox\',
\'clients\'
);
...
//Visibility
add_meta_box(
\'clients_visible\',
__( \'Show on clients page\' ),
\'infotravel_clients_visible_metabox\',
\'clients\'
);
}
add_action( \'add_meta_boxes\', \'clients_custom_metaboxes\' );
function infotravel_clients_street_metabox( $post ) {
wp_nonce_field( \'clients_street\', \'clients_street_nonce\' );
$value = get_post_meta( $post->ID, \'_clients_street\', true );
?>
<input class="trek_inputs required" value="<?php echo $value ?>" type="text" id="txtClientsStreet" name="txtClientsStreet" maxlength="200" required placeholder="Street address" />
<?php
}
function infotravel_clients_visible_metabox( $post ) {
wp_nonce_field( \'clients_show\', \'clients_show_nonce\' );
$value = get_post_meta( $post->ID, \'_clients_show\', true );
?>
<input type="checkbox" id="chkClientShow" name="chkClientShow" value="<?php echo $value ?>" />
<label for="chkClientShow">Show this client on Clients page</label>
<?php
}
function infotravel_save_clients_meta( $post_id ) {
if(
! isset( $_POST[\'clients_street_nonce\'] ) ||
! isset( $_POST[\'clients_show_nonce\'] ) ) {
return;
}
if( ! current_user_can( \'edit_post\', $post_id) ) {
return;
}
// mandatory fields
$street_address = sanitize_text_field( $_POST[\'txtClientsStreet\'] );
$visible = sanitize_text_field( $_POST[\'chkClientShow\'] );
$short_address = $street_address . \', \' . $city . \', \' . $state;
$full_address = $short_address . \', \' . $zip . \', \' . $country;
/**
* Update meta information
*/
update_post_meta( $post_id, \'_clients_street\', $street_address );
update_post_meta( $post_id, \'_clients_show\', $visible );
update_post_meta( $post_id, \'_clients_short_address\', $short_address );
update_post_meta( $post_id, \'_clients_full_address\', $full_address );
}
add_action( \'save_post\', \'infotravel_save_clients_meta\' );
我错过了什么?
数据库屏幕截图
Further Testing
我增加了一个值(
xxxxxxx
) 在里面
_clients_show
字段并检查我在
metabox
创建时间:
function infotravel_clients_visible_metabox( $post ) {
wp_nonce_field( \'clients_show\', \'clients_show_nonce\' );
$value = get_post_meta( $post->ID, \'_clients_show\', true );
print_r($value);
?>
<input type="checkbox" id="chkClientShow" name="chkClientShow" checked="checked" />
<label for="chkClientShow">Show this client on Clients page</label>
<?php
}
令人惊讶的是,该值没有显示!!
最合适的回答,由SO网友:nmr 整理而成
在我看来,问题在于您操作复选框的方式。作为复选框值,可以设置从数据库读取的值,该值有时是空字符串。
发送未选中的复选框时,没有$_POST[\'chkClientShow\']
键和空字符串保存到数据库中。然后将空字符串设置为复选框值。从现在起,自定义字段_clients_show
始终为空字符串。
以下代码应该可以工作
function infotravel_clients_visible_metabox( $post ) {
wp_nonce_field( \'clients_show\', \'clients_show_nonce\' );
$value = get_post_meta( $post->ID, \'_clients_show\', true );
$is_checked = ($value == 1) ? \'checked\' : \'\';
?>
<input type="checkbox" id="chkClientShow" name="chkClientShow" value="1" <?php echo $is_checked; ?>/>
<label for="chkClientShow">Show this client on Clients page</label>
<?php
}
function infotravel_save_clients_meta( $post_id )
{
//
// ...
//
$visible = isset( $_POST[\'chkClientShow\'] ) && $_POST[\'chkClientShow\'] == 1;
$visible = (int)$visible;
update_post_meta( $post_id, \'_clients_show\', $visible );
//
// ...
//
}