Custom field being erased after autosave

Why wont the custom fields from this post type save?

I have a custom post-type called Photos and a couple of meta boxes. Here is basically what I have in my functions.php

add_action('init', 'photo_register');
 
function photo_register() {
 
    $labels = array(
        'name' => _x('My Photos', 'post type general name'),
        'singular_name' => _x('Photo', 'post type singular name'),
        'add_new' => _x('Add New', 'photo item'),
        'add_new_item' => __('Add New Photo'),
        'edit_item' => __('Edit Photo'),
        'new_item' => __('New Photo'),
        'view_item' => __('View Photo'),
        'search_items' => __('Search Photos'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );
    
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        //'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 4,
        'taxonomies' => array('post_tag','category'),
        'supports' => array('title','comments','trackbacks','revisions','custom-fields','page-attributes','thumbnail', 'excerpt', 'tags')
      ); 
 
    register_post_type( 'photo' , $args );
    register_taxonomy("Photos", array("photo"), array("hierarchical" => true, "label" => "Photos", "singular_label" => "Photo", "rewrite" => true));

    
}

add_action("admin_init", "admin_init");
add_action('save_post', 'save_details');
 
function admin_init(){ // add_meta_box( $id, $title, $callback, $page, $context, $priority ); 
  add_meta_box("media", "Media Type", "media", "photo", "side", "high");
  add_meta_box("map_meta", "Mapping Info", "map_meta", "photo", "normal", "high");
  add_meta_box("photo_meta", "Add a photograph", "photo_meta", "photo", "normal", "high");
}
 
function media(){
  global $post;
  $custom = get_post_custom($post->ID);
  $film = $custom["film"][0];
  $camera = $custom["camera"][0];
 
  ?>
  <label>Camera:</label>
  <input name="camera" value="<?php echo $camera; ?>" />
  
    <label>Film:</label>
  <input name="film" value="<?php echo $film; ?>" />
  
  <?php
}

function photo_meta(){
  global $post;
  $custom = get_post_custom($post->ID);
  $single_photo = $custom["single_photo"][0];
  ?>
  
  <div id="singleUpload">

    <div class="sUinput">
        <input id="single_photo" name="single_photo" value="<?php echo $single_photo; ?>" />
    </div>
    <div class="sUbutton">
    <input type="button" value="Upload" name="upload" id="upload_image_button" />
    </div>
  </div>
  
  <?php
}


function map_meta() {
  global $post;
  $custom = get_post_custom($post->ID);
  $latitude = $custom["latitude"][0];
  $longitude = $custom["longitude"][0];

  ?>
<div id="mapControls">

<div class="lat">
  <p><label for="lat">Latitude:</label><br />
  <input id="latitude"  name="latitude" value="<?php echo $latitude; ?>"></input></p>
 </div>
 <div class="lng">
  <p><label>Longitude:</label><br />
  <input id="longitude" name="longitude" value="<?php echo $longitude; ?>" ></input></p>
 </div>

   <?php }

  function save_details(){
  global $post;
  update_post_meta($post->ID, "single_photo", $_POST["single_photo"]);
  update_post_meta($post->ID, "latitude", $_POST["latitude"]);
  update_post_meta($post->ID, "longitude", $_POST["longitude"]);
  update_post_meta($post->ID, "camera", $_POST["camera"]);
  update_post_meta($post->ID, "film", $_POST["film"]);
 
}

I had it working for a while but now it will not save the fields and a refresh wipes everything. Can anyone see what the problem is? Any suggestions on how to find the bug? Basically anything you can think to improve this would be much appreciated, especially if I can get it to work!

EDIT

I figured out that they do save but if I remain on a post long enough for an autosave to run, then try to navigate away I get the message :

The page is asking you to confirm that you want to leave – data you
have entered may not be saved.

I click leave then when I return to the post everything is erased. What is going on here?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I use the following code to prevent updates of my custom fields during auto-saves, ajax requests (quick edit) and bulk edits.

add_action('save_post', 'save_my_post');
function save_my_post($post_id)
{
    // Stop WP from clearing custom fields on autosave,
    // and also during ajax requests (e.g. quick edit) and bulk edits.
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit']))
        return;

    // Clean, validate and save custom fields
}

Also see: How to prevent custom fields from being cleared during a bulk edit?

Method 2

You need to ignore all autosave requests. Modify your save function like this:

function save_details(){
   if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

   global $post;
   update_post_meta($post->ID, "single_photo", $_POST["single_photo"]);
   update_post_meta($post->ID, "latitude", $_POST["latitude"]);
   update_post_meta($post->ID, "longitude", $_POST["longitude"]);
   update_post_meta($post->ID, "camera", $_POST["camera"]);
   update_post_meta($post->ID, "film", $_POST["film"]);
}

Also, you should definitely add a bit more validation so that the function cannot be called in situations other than what it is intended for. I usually validate post type, user permissions, and nonces.

Method 3

You need to isolate your save_post action. You also aren’t passing in the post_id as an argument to the function.

function save_details($post_id){
    $post =& $_REQUEST;
    if($post['post_type'] == 'post'){
        update_post_meta($post_id, "single_photo", $_POST["single_photo"]);
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x