在我的自定义帖子类型(“kvkoolitus”)编辑页面上有两个元数据库(“价格”和“日期”)。正如我所想的那样,wordpress只保存第一个渲染的元盒中的值。每个元盒代码存储在各自的文件中。如果我只包含1个元盒-它可以工作,如果两者都包含-wordpress只保存第一个渲染的元盒中的值
我错过了什么?
第一个metabox创建函数:
function kvkoolitus_prices_metabox() {
add_meta_box(
\'kvkoolitus_prices_metabox\',
__( \'Course Price\', \'kvkoolitus\'),
\'kvkoolitus_prices_metabox_callback\',
\'kvkoolitus\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes_kvkoolitus\', \'kvkoolitus_prices_metabox\' );
第二个metabox创建函数:
function kvkoolitus_dates_metabox() {
add_meta_box(
\'kvkoolitus_dates_metabox\',
__( \'Course Time\', \'kvkoolitus\'),
\'kvkoolitus_dates_metabox_callback\',
\'kvkoolitus\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes_kvkoolitus\', \'kvkoolitus_dates_metabox\' );
第一个metabox回调函数:
function kvkoolitus_dates_metabox_callback( $post ) {
wp_nonce_field( \'kvkoolitus_dates_metabox_nonce\', \'kvkoolitus_dates_nonce\' ); ?>
<form action="" method="post">
<?php
$start_date = get_post_meta( $post->ID, \'kvkoolitus-start\', true );
$end_date = get_post_meta( $post->ID, \'kvkoolitus-end\', true );
$duration = get_post_meta( $post->ID, \'kvkoolitus-duration\', true );
?>
<p>
<label for="kvkoolitus_start_date"><?php _e(\'Start date\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_start_date" value="<?php echo esc_attr( $start_date ); ?>" />
</p>
<p>
<label for="kvkoolitus_end_date"><?php _e(\'End date\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_end_date" value="<?php echo esc_attr( $end_date ); ?>" />
</p>
<p>
<label for="kvkoolitus_duration"><?php _e(\'Duration\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat" name="kvkoolitus_duration" value="<?php echo esc_attr( $duration ); ?>" />
</p>
</form>
<?php }
第二个metabox回调函数:
function kvkoolitus_prices_metabox_callback( $post ) {
wp_nonce_field( \'kvkoolitus_prices_metabox_nonce\', \'kvkoolitus_prices_nonce\' ); ?>
<form action="" method="post">
<?php
$price = get_post_meta( $post->ID, \'kvkoolitus-price\', true );
$km = get_post_meta( $post->ID, \'kvkoolitus-km\', true );
?>
<p>
<label for="kvkoolitus_price"><?php _e(\'Price\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat" name="kvkoolitus_price" value="<?php echo esc_attr( $price ); ?>" />
</p>
<p>
<input type="checkbox" value="1" name="kvkoolitus_km" <?php checked( \'1\', $km ); ?> />
<label for="kvkoolitus_km"><?php _e(\'Income Tax Included\', \'kvkoolitus\' ); ?></label><br/>
</p>
</form>
<?php }
第一个metabox保存功能:
function kvkoolitus_dates_save_meta( $post_id ) {
if( !isset( $_POST[\'kvkoolitus_dates_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_dates_nonce\'],\'kvkoolitus_dates_metabox_nonce\') )
return;
if ( !current_user_can( \'edit_post\', $post_id ))
return;
if ( isset($_POST[\'kvkoolitus_start_date\']) ) {
update_post_meta($post_id, \'kvkoolitus-start\', sanitize_text_field( $_POST[\'kvkoolitus_start_date\']));
}
if ( isset($_POST[\'kvkoolitus_end_date\']) ) {
update_post_meta($post_id, \'kvkoolitus-end\', sanitize_text_field( $_POST[\'kvkoolitus_end_date\'] ));
}
if ( isset($_POST[\'kvkoolitus_duration\']) ) {
update_post_meta($post_id, \'kvkoolitus-duration\', sanitize_text_field($_POST[\'kvkoolitus_duration\']));
}
}
add_action(\'save_post\', \'kvkoolitus_dates_save_meta\');
第二个metabox保存功能:
function kvkoolitus_prices_save_meta( $post_id ) {
if( !isset( $_POST[\'kvkoolitus_prices_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_prices_nonce\'],\'kvkoolitus_prices_metabox_nonce\') )
return;
if ( !current_user_can( \'edit_post\', $post_id ))
return;
if ( isset($_POST[\'kvkoolitus_price\']) ) {
update_post_meta($post_id, \'kvkoolitus-price\', sanitize_text_field($_POST[\'kvkoolitus_price\']));
}
if ( isset($_POST[\'kvkoolitus_km\']) ) {
update_post_meta($post_id, \'kvkoolitus-km\', sanitize_text_field($_POST[\'kvkoolitus_km\']));
}
}
add_action(\'save_post\', \'kvkoolitus_prices_save_meta\');
最合适的回答,由SO网友:cybmeta 整理而成
首先,你不需要<form>
标签;元框已在编辑后表单中呈现。
其次,如果未选中复选框,则不会发送该复选框,因此isset( $_POST[\'checkbox_name\'] )
回来false
并且存储复选框值的自定义字段未正确更新。
我已经用standard post 如果将其与自定义帖子类型一起使用,则它也必须有效:
function kvkoolitus_prices_metabox() {
add_meta_box(
\'kvkoolitus_prices_metabox\',
__( \'Course Price\', \'kvkoolitus\'),
\'kvkoolitus_prices_metabox_callback\',
\'post\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes\', \'kvkoolitus_prices_metabox\' );
function kvkoolitus_dates_metabox() {
add_meta_box(
\'kvkoolitus_dates_metabox\',
__( \'Course Time\', \'kvkoolitus\'),
\'kvkoolitus_dates_metabox_callback\',
\'post\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes\', \'kvkoolitus_dates_metabox\' );
function kvkoolitus_dates_metabox_callback( $post ) {
wp_nonce_field( \'kvkoolitus_dates_metabox_nonce\', \'kvkoolitus_dates_nonce\' ); ?>
<?php
$start_date = get_post_meta( $post->ID, \'kvkoolitus-start\', true );
$end_date = get_post_meta( $post->ID, \'kvkoolitus-end\', true );
$duration = get_post_meta( $post->ID, \'kvkoolitus-duration\', true );
?>
<p>
<label for="kvkoolitus_start_date"><?php _e(\'Start date\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_start_date" value="<?php echo esc_attr( $start_date ); ?>" />
</p>
<p>
<label for="kvkoolitus_end_date"><?php _e(\'End date\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_end_date" value="<?php echo esc_attr( $end_date ); ?>" />
</p>
<p>
<label for="kvkoolitus_duration"><?php _e(\'Duration\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat" name="kvkoolitus_duration" value="<?php echo esc_attr( $duration ); ?>" />
</p>
<?php }
function kvkoolitus_prices_metabox_callback( $post ) {
wp_nonce_field( \'kvkoolitus_prices_metabox_nonce\', \'kvkoolitus_prices_nonce\' ); ?>
<?php
$price = get_post_meta( $post->ID, \'kvkoolitus-price\', true );
$km = get_post_meta( $post->ID, \'kvkoolitus-km\', true );
?>
<p>
<label for="kvkoolitus_price"><?php _e(\'Price\', \'kvkoolitus\' ); ?></label><br/>
<input type="text" class="widefat" name="kvkoolitus_price" value="<?php echo esc_attr( $price ); ?>" />
</p>
<p>
<input type="checkbox" value="1" name="kvkoolitus_km" <?php checked( \'1\', $km ); ?> />
<label for="kvkoolitus_km"><?php _e(\'Income Tax Included\', \'kvkoolitus\' ); ?></label><br/>
</p>
<?php }
function kvkoolitus_dates_save_meta( $post_id ) {
if( !isset( $_POST[\'kvkoolitus_dates_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_dates_nonce\'],\'kvkoolitus_dates_metabox_nonce\') )
return;
if ( !current_user_can( \'edit_post\', $post_id ))
return;
if ( isset($_POST[\'kvkoolitus_start_date\']) ) {
update_post_meta($post_id, \'kvkoolitus-start\', sanitize_text_field( $_POST[\'kvkoolitus_start_date\']));
}
if ( isset($_POST[\'kvkoolitus_end_date\']) ) {
update_post_meta($post_id, \'kvkoolitus-end\', sanitize_text_field( $_POST[\'kvkoolitus_end_date\'] ));
}
if ( isset($_POST[\'kvkoolitus_duration\']) ) {
update_post_meta($post_id, \'kvkoolitus-duration\', sanitize_text_field($_POST[\'kvkoolitus_duration\']));
}
}
add_action(\'save_post\', \'kvkoolitus_dates_save_meta\');
function kvkoolitus_prices_save_meta( $post_id ) {
if( !isset( $_POST[\'kvkoolitus_prices_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_prices_nonce\'],\'kvkoolitus_prices_metabox_nonce\') )
return;
if ( !current_user_can( \'edit_post\', $post_id ))
return;
if ( isset($_POST[\'kvkoolitus_price\']) ) {
update_post_meta($post_id, \'kvkoolitus-price\', sanitize_text_field($_POST[\'kvkoolitus_price\']));
}
if ( isset($_POST[\'kvkoolitus_km\']) ) {
update_post_meta($post_id, \'kvkoolitus-km\', sanitize_text_field($_POST[\'kvkoolitus_km\']));
} else {
delete_post_meta($post_id, \'kvkoolitus-km\');
}
}
add_action(\'save_post\', \'kvkoolitus_prices_save_meta\');