WordPress.org

Forums

[resolved] Losing custom plugin data when restoring autosave (2 posts)

  1. tonnaer
    Member
    Posted 2 years ago #

    I have a problem similar to the ones described in these posts:
    http://wordpress.org/support/topic/autosave-problem-with-custom-meta-box?replies=3
    http://wordpress.org/support/topic/custom-fields-lost-by-autosave?replies=14

    I solved it partly by adding the check for autosave in the function I call in the save_post hook:

    function my_function($pID) {
    
      // if the function is called by the WP autosave feature, nothing must be saved
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;
    
      $text = (isset($_POST['textfield1'])) ? $_POST['textfield1'] : '';
      update_post_meta($pID, '_textfield1', $text);
      $text = (isset($_POST['textfield2'])) ? $_POST['textfield2'] : '';
      update_post_meta($pID, '_textfield2', $text);
    }
    add_action('save_post', 'my_function');

    Now if I leave the edit post screen without updating after a draft has been saved, and then I reenter the edit post screen, my custom plugin data is still there.

    But, if I decide to restore the saved draft, my plugin's text fields are empty, all data lost.

    What am I missing? How can I prevend the loss of my plugin data?

  2. tonnaer
    Member
    Posted 2 years ago #

    After a couple of hours of searching, I seem to have found the answer.
    I've added the nonce field verification (as in the example on this codex page: http://codex.wordpress.org/Function_Reference/add_meta_box) and now the values don't disappear when I restore an autosave.

Topic Closed

This topic has been closed to new replies.

About this Topic