保存页面后,我的Metabox数据不会显示,我在getextra\\u mbe\\u date和getextra\\u mbe\\u time上都尝试了vardump(),返回null
我的代码是
<?php
/*
Plugin Name: Events CPT
Plugin URI: ****
Description: This Plugins creates an events custom post type with a date and time metabox included.
Version: 0.01
Author: ***
Author URI: ****
License: GPLv2
*/
?>
<?php
function getextra_events_custom_post_type_create() {
$labels = array(
\'name\' => \'Events\',
\'singular_name\' => \'Event\',
\'add_new\' => \'Add New Event\',
\'add_new_item\' => \'Add New Event\',
\'edit_item\' => \'Edit Event\',
\'new_item\' => \'New Event\',
\'all_items\' => \'All Events\',
\'view_item\' => \'View Event\',
\'search_items\' => \'Search Events\',
\'not_found\' => \'No Events found\',
\'not_found_in_trash\' => \'No Eventss found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Events\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'events\' ),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
);
register_post_type( \'Events\', $args );
}
add_action( \'init\', \'getextra_events_custom_post_type_create\' );
?>
<?php
//creation of the metabox happens here
add_action( \'add_meta_boxes\', \'getextra_mbe_create\' );
function getextra_mbe_create() {
add_meta_box( \'getextra-meta-dt\', \'Event Date and Time\', \'getextra_mbe_dt_callback\', \'Events\', \'normal\', \'high\');
}
//function to display the form fields
function getextra_mbe_dt_callback($post) {
//get the metadata values if they exist
$getextra_mbe_date = get_post_meta ($post->ID, \'_getextra_mbe_date\', true);
$getextra_mbe_time = get_post_meta ($post->ID, \'_getextra_mbe_time\', true);
echo \'Please fill out the event time and date.\';
?>
<p>Date: <input type="text" name="getextra_mbe_date" value="<?php esc_attr( $getextra_mbe_date); ?>" /></p>
<p>Time: <input type="text" name="getextra_mbe_time" value="<?php esc_attr( $getextra_mbe_time); ?>" /> </p>
<?php }
//hook to save the meta box data
add_action (\'save_post\', \'getextra_mbe_save_dt\');
function getextra_mbe_save_dt( $post_id ) {
//verify the metadata is set
if (isset( $_POST[\'getextra_mbe_date\'])) {
//save the metadata
update_post_meta ($post_id, \'_getextra_mbe_date\', strip_tags($_POST[\'getextra_mbe_date\']));
update_post_meta( $post_id, \'_getextra_mbe_time\', strip_tags($_POST[\'getextra_mbe_time\']));
}
}
?>