0

My Metabox data doesn't appear once I've saved the page, I tried vardump() on both getextra_mbe_date and getextra_mbe_time which returned null

my code is

<?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']));
    }

}
 ?>
1
  • 1
    What else have you tried to debug it? Maybe try echoing out your $_POST variables then use die() to see what comes out to make sure you even entering your if statement.
    – Howdy_McGee
    Commented Feb 11, 2014 at 17:19

1 Answer 1

0

I tried your code and it saved the custom fields just fine. The issue is that esc_attr returns a string but does not echo it out.

Try this instead:

<p>Date: <input type="text" name="getextra_mbe_date" value="<?php echo esc_attr( $getextra_mbe_date); ?>" /></p>
<p>Time: <input type="text" name="getextra_mbe_time" value="<?php echo esc_attr( $getextra_mbe_time); ?>" /> </p> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.