WordPress.org

Ready to get started?Download WordPress

Forums

Problems with autosave and custom fields (3 posts)

  1. coopersita
    Member
    Posted 4 years ago #

    I added some custom boxes for customs fields via add_meta_box, but I'm having problems with autosave.

    I read this post. And I thought I had fixed my problem, but now I see that I can't save my custom fields at all. Here is a sample of what I did (I have more than one custom box):

    I added the fields with the hidden field at the end:

    add_meta_box("pstatus", "Status", "pstatus", "projects", "side", "low");
    function pstatus(){
      global $post;
      $custom = get_post_custom($post->ID);
      $status = $custom["pstatus"][0];
      $dcompleted = $custom["dcompleted"][0]
      ?>
      <p> <input type="radio" name="pstatus" value="inprogress" <?php if ($status=="inprogress" || $status == '') echo "checked=checked"; ?> /> In Production </p>
      <p> <input type="radio" name="pstatus" value="completed" <?php if ($status=="completed") echo "checked=checked"; ?> /> Completed: <input class="date-pick" type="text" name="dcompleted" value="<?php  echo $dcompleted; ?>" /></p>
    
      <?php
      echo '<input type="hidden" name="status_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    }

    The save function:

    add_action('save_post', 'save_details');
    function save_details(){
      global $post;
      $post_id = $post->ID;
    
       if (!wp_verify_nonce($_POST['status_nonce'], basename(__FILE__))) {
    	        return $post_id;
      }
    
     if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE )
    		return $post_id;
    
      update_post_meta($post->ID, "pstatus", $_POST["pstatus"]);
      update_post_meta($post->ID, "dcompleted", $_POST["dcompleted"]);
    
    }

    The part that seems to be messing it up is:

    if (!wp_verify_nonce($_POST['status_nonce'], basename(__FILE__))) {
    	        return $post_id;
      }

    If I comment that out, then I can save as normal.

    What am I doing wrong? Is the hidden field not set up properly?

    Thanks

  2. coopersita
    Member
    Posted 4 years ago #

    No one?

  3. simplethemes
    Member
    Posted 3 years ago #

    I've been wrestling with the same issue.
    I think you're problem here is the "basename".

    For the add_meta_box function:

    echo '<input type="hidden" name="price_noncename" id="status_nonce" value="' . wp_create_nonce( 'status_nonce'.$post->ID ) . '" />';

    And for the save function:

    if ( !wp_verify_nonce( $_POST['status_nonce'], 'status'.$post_id )) {
            return $post_id;
          }

Topic Closed

This topic has been closed to new replies.

About this Topic