Search code examples
wordpressautosave

Doesn't work auto save custom fields in Wordpress


I'm using Smashing Magazine tutorial step by step. But there is doesn't work auto save in my custom fields. After click publish and update button the fields are empty. How to resolve the issue.

 function my_custom_post_movie() {
  $labels = array(
    'name'               => _x( 'movies', 'post type general name' ),
    'singular_name'      => _x( 'movie', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New movie' ),
    'edit_item'          => __( 'Edit movie' ),
    'new_item'           => __( 'New movie' ),
    'all_items'          => __( 'All movies' ),
    'view_item'          => __( 'View movie' ),
    'search_items'       => __( 'Search movies' ),
    'not_found'          => __( 'No movies found' ),
    'not_found_in_trash' => __( 'No movies found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'movies'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our movies and movie specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'movie', $args ); 
}
add_action( 'init', 'my_custom_post_movie' );

// Register Custom Taxonomies
function my_taxonomies_movie() {
  $labels = array(
    'name'              => _x( 'movie Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'movie Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search movie Categories' ),
    'all_items'         => __( 'All movie Categories' ),
    'parent_item'       => __( 'Parent movie Category' ),
    'parent_item_colon' => __( 'Parent movie Category:' ),
    'edit_item'         => __( 'Edit movie Category' ), 
    'update_item'       => __( 'Update movie Category' ),
    'add_new_item'      => __( 'Add New movie Category' ),
    'new_item_name'     => __( 'New movie Category' ),
    'menu_name'         => __( 'movie Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'movie_category', 'movie', $args );
}
add_action( 'init', 'my_taxonomies_movie', 0 );

// Meta Box
add_action( 'add_meta_boxes', 'movie_date_box' );
function movie_date_box() {
    add_meta_box( 
        'movie_date_box',
        __( 'movie date', 'myplugin_textdomain' ),
        'movie_date_box_content',
        'movie',
        'side',
        'high'
    );
}

function movie_date_box_content( $post ) {
  wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
  echo '<label for="movie_date"></label>';
  echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}

add_action( 'save_post', 'movie_date_box_save' );
function movie_date_box_save( $post_id ) {

   if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

  if ( !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
  return;

  if ( 'page' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_page', $post_id ) )
    return;
  } else {
    if ( !current_user_can( 'edit_post', $post_id ) )
    return;
  }
  $movie_date = $_POST['movie_date'];
  update_post_meta( $post_id, 'movie_date', $movie_date );
}

Thanks for the help.


Solution

  • Your information is in fact being saved to the database, but it is not being called once the post has been submitted.

    Change:

    function movie_date_box_content( $post ) {
      wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
      echo '<label for="movie_date"></label>';
      echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
    }
    

    to the following:

    function movie_date_box_content( $post ) {
      wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
      echo '<label for="movie_date"></label>';
      echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" value="' . get_post_meta( $post->ID, 'movie_date', true ) . '" />';
    }
    

    You were missing value="' . get_post_meta( $post->ID, 'movie_date', true ) . '"

    get_post_meta() calls the meta key movie_date from the database and shows it, if there is anything stored in that key.

    Also change:

    if ( !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
    return;
    

    to the following:

    if ( !isset($_POST['movie_date_box_content_nonce']) || !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
        return;
    

    This checks to see if the nonce field is set and not NULL. It takes care of the Undeclared Index notice that shows up in the admin.