10

The code below is working nearly flawlessly, however my value for page title on one of my pages keeps coming up empty after a few page refreshes... It sticks for awhile, then it appears to reset to empty. I'm thinking I must have a conflict in the code below, but I can't quite figure it.

I'm allowing the user to set a custom page title for posts as well as pages via a custom "post/page title input field). Can anyone see an obvious issue here that might be resetting the page title to blank?

// ===================
// = POST OPTION BOX =
// ===================

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
    if ( function_exists('add_meta_box') ) { 
      //add_meta_box( $id, $title, $callback, $page, $context, $priority );
        add_meta_box('post_header', 'Custom Post Header Code (optional)', 'custom_post_images', 'post', 'normal', 'low');
        add_meta_box('post_title', 'Custom Post Title', 'custom_post_title', 'post', 'normal', 'high');
        add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title', 'page', 'normal', 'high');
        add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
        add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', 'side', 'core');
    }
}

//Adds the custom images box
function custom_post_images() {
    global $post;
    ?>
    <div class="inside">
        <textarea style="height:70px; width:100%;margin-left:-5px;" name="customHeader" id="customHeader"><?php echo get_post_meta($post->ID, 'customHeader', true); ?></textarea>
        <p>Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing <b>for this post only</b>. You can enter image references like so &lt;img src='wp-content/uploads/product1.jpg' /&gt;. To show default images, just leave this field empty</p>
    </div>
<?php
}

//Adds the custom post title box
function custom_post_title() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="customTitle" id="customTitle" value="<?php echo get_post_meta($post->ID, 'customTitle', true); ?>"></p>
        <p>Enter your custom post/page title here and it will be used for the html &lt;title&gt; for this post page and the Google link text used for this page.</p>
    </div>
<?php
}


add_action('save_post', 'custom_add_save');

function custom_add_save($postID){
    if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved and not on autosave
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['customHeader']) 
        {
            update_custom_meta($postID, $_POST['customHeader'], 'customHeader');
        }
        else
        {
            update_custom_meta($postID, '', 'customHeader');
        }
        if ($_POST['customTitle']) 
        {
            update_custom_meta($postID, $_POST['customTitle'], 'customTitle');
        }
        else
        {
            update_custom_meta($postID, '', 'customTitle');
        }
    }

  }
    function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
}
?>
2
  • Might be something to do with Wordpress's auto save system, try checking if $_POST['action'] equals autosave in your custom_add_save function.
    – Richard M
    Mar 29, 2010 at 19:38
  • I had not thought of that Richard, but I think it might be a contributing factor (even though the custom post title is never empty in these cases where it seems to get reset to empty. I'm not clear about how you are suggesting to check the autosave in the if/then blocks. Can you post a bit of sample code?
    – Scott B
    Mar 29, 2010 at 20:34

3 Answers 3

17

Wordpress's auto save system may well be your problem, as I think custom fields are not passed along for auto saves (so your customHeader and customTitle post variables will be empty during an auto save).

In your save function you should check if the DOING_AUTOSAVE constant is set (this seems to be preferable to checking the post action) and return if so. Something like this:

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

See this ticket for more info: http://core.trac.wordpress.org/ticket/10744

2
  • Richard, I'm still a bit unclear as to how to implement this, but I've updated my question with the revised code. Can you have a look and let me know if this is a valid workaround to avoid the empty post meta that autosave appears to induce?
    – Scott B
    Mar 29, 2010 at 21:35
  • Yes, it looks OK. Although, you don't really need the else branch of your condition, just check for the constant and return if it's set.
    – Richard M
    Mar 30, 2010 at 15:55
4

The WordPress Codex has got an function reference for add_meta_box(), with a great example.

And yes, it uses


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

and I think you've implemented it the right way.

2
  • Thanks Jocap. I appreciate the confirmation. Richard M had already sent the info on the autosave issue, I just forgot to accept his answer. It's working flawlessly since :)
    – Scott B
    Mar 30, 2010 at 15:26
  • Good to hear, Scott. Good luck! :)
    – jocap
    Mar 30, 2010 at 15:31
0

FYI, the solution posted here http://wordpress.org/support/topic/custom-post-type-information-disappearing worked for me and I think is much more elegant.

1
  • 3
    Links to external sites aren't acceptable answers. It's unsearchable, and becomes invalid if something happens to the off-site reference. If you feel the need to post nothing else but an outside link, please make it a comment to the original question rather than an answer.
    – Ken White
    Jun 29, 2011 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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