我正在为一位朋友的WordPress网站上创建一个相当简单的自定义页面,但我在正确保存元数据库的内容时遇到了一些实际困难。WordPress的行为似乎不像我所期望的PHP那样,因此,当我将echo语句放在某个地方以查看它的位置时,它们并不总是出现,即使代码必须运行时也是如此。非常奇怪。
有人能指出我做错了什么吗?(很抱歉代码太多!)
function metabox_exhibition_setup() {
/* Add \'start_date\' hook. */
add_meta_box(\'gc_start_date\',
esc_html__(\'Exhibition Start Date\'),
\'create_metabox_exhibition_start_date\',
\'gc_exhibition\',
\'side\',
\'default\');
/* Add \'finish_date\' hook. */
add_meta_box(\'gc_finish_date\',
esc_html__(\'Exhibition Finish Date\'),
\'create_metabox_exhibition_finish_date\',
\'gc_exhibition\',
\'side\',
\'default\');
/* Add \'subtitle\' hook. */
add_meta_box(\'gc_subtitle\',
esc_html__(\'Exhibition Subtitle\'),
\'create_metabox_exhibition_subtitle\',
\'gc_exhibition\',
\'normal\',
\'core\');
add_action( \'save_post\', \'save_metabox_exhibition\', 10, 2 );
}
/* Save the meta box\'s post metadata. */
function save_metabox_exhibition( $post_id, $post ) {
$fields = 0;
if($_POST) {
$metaboxes[0][\'nonce\'] = $_POST[\'exhibition_finish_date_nonce\'];
$metaboxes[0][\'data\'] = $_POST[\'gc_finish_date\'];
$metaboxes[1][\'nonce\'] = $_POST[\'exhibition_start_date_nonce\'];
$metaboxes[1][\'data\'] = $_POST[\'gc_start_date\'];
$metaboxes[2][\'nonce\'] = $_POST[\'exhibition_subtitle_nonce\'];
$metaboxes[2][\'data\'] = $_POST[\'gc_subtitle\'];
$fields = count($metaboxes);
$fields = count($metaboxes);
}
//print_r($metaboxes);
for($i = 0; $i <= $fields ; $i++ ) {
/* Verify the nonce before proceeding. */
if ( !isset( $metaboxes[$i][\'nonce\'] ) || !wp_verify_nonce( $metaboxes[$i][\'nonce\'], basename( __FILE__ ) ) )
return $post_id;
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
/* Get the posted data and sanitize it */
$new_meta_value = ( isset( $metaboxes[$i][\'data\'] ) ? sanitize_html_class( $metaboxes[$i][\'data\'] ) : \'\' );
/* Get the meta key. */
$meta_key = $metaboxes[$i][\'data\'];
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && \'\' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( \'\' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}
SO网友:Django Reinhardt
啊哈!愚蠢的错误。我没有给它一个正确的元键!
这似乎有固定的东西:
/* Save the meta box\'s post metadata. */
function save_metabox_exhibition( $post_id, $post ) {
$fields = 0;
if($_POST) {
$metaboxes[0][\'nonce\'] = $_POST[\'exhibition_finish_date_nonce\'];
$metaboxes[0][\'data\'] = $_POST[\'gc_finish_date\'];
$metaboxes[0][\'meta\'] = \'gc_finish_date\';
$metaboxes[1][\'nonce\'] = $_POST[\'exhibition_start_date_nonce\'];
$metaboxes[1][\'data\'] = $_POST[\'gc_start_date\'];
$metaboxes[1][\'meta\'] = \'gc_start_date\';
$metaboxes[2][\'nonce\'] = $_POST[\'exhibition_subtitle_nonce\'];
$metaboxes[2][\'data\'] = $_POST[\'gc_subtitle\'];
$metaboxes[2][\'meta\'] = \'gc_subtitle\';
$fields = count($metaboxes);
}
for($i = 0; $i <= $fields ; $i++ ) {
/* Verify the nonce before proceeding. */
if ( !isset( $metaboxes[$i][\'nonce\'] ) || !wp_verify_nonce( $metaboxes[$i][\'nonce\'], basename( __FILE__ ) ) )
return $post_id;
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
/* Get the posted data and sanitize it */
$new_meta_value = ( isset( $metaboxes[$i][\'data\'] ) ? sanitize_text_field( $metaboxes[$i][\'data\'] ) : \'\' );
/* Get the meta key. */
$meta_key = $metaboxes[$i][\'meta\'];
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && \'\' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( \'\' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}