Why do I lose the content of meta boxes when I leave the page?

So I’ve been coding a theme and I’m working on a custom post type. When I edit a custom post and save the changes (click Update) everything is ok.

But – I noticed that when I leave the Edit page open for a while and come back to it a few minutes later and try to refresh, that dialog “Stay On Page/Leave Page” pops up and if I choose “leave page” all of the content from the custom meta boxes is lost. Only the title remains.

Read More

I’m guessing this has something to do with the way that I’m saving the data from the custom meta boxes, so here it is:

add_action('admin_init', 'init_portfolio_meta_boxes');

function init_portfolio_meta_boxes() {
    add_meta_box('short_description', 'Short Description', 'short_description_meta', 'portfolio', 'normal');
}

function short_description_meta() {
    global $post;
    $custom = get_post_custom($post->ID);
    $short_description = $custom['short_description'][0];
    ?>
        <textarea name="short_description" style="width: 200px; height: 100px;"><?php echo $short_description; ?></textarea>
    <?php
}

add_action('save_post', 'save_details');
function save_details() {
    global $post;
    update_post_meta($post->ID, 'short_description', $_POST['short_description']);
}

That’s the entire code for one of the meta boxes, I followed a tutorial to get to it.

Related posts

Leave a Reply

1 comment

  1. This is because save_details is not checking if its an autosave routine:

    add_action('save_post', 'save_details');
    function save_details() {
        global $post;
        // verify this is not an auto save routine. 
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
        update_post_meta($post->ID, 'short_description', $_POST['short_description']);
    }