Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm not quite sure why but I've been playing around with all kinds of different solutions but none seem to work. I've traded update_post_meta with add_post_meta and even broke it up into 3 conditionals to see if it was adding, updating, or deleting but nothing seems to work.

The problem is that it won't actually save and show the data on update. I know that '_desc' is set since it does show data when I die() (right before the update). Suggestions on how I can figure out why it's not updating?

/** Add the Meta Box **/
function add_custom_meta_box() {
    global $meta_box;
    add_meta_box(
        'short-desc', // $id
        'Short Description', // $title 
        'show_custom_meta_box', // $callback
        'post', // $page
        'side', // $context
        'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');

/** The Callback **/
function show_custom_meta_box() {
    global $post;
    // Use nonce for verification
    echo '<input type="hidden" name="shortdesc_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // get value of this field if it exists for this post
    $meta = get_post_custom($post->ID);

    // Begin the field table
    echo '<table class="form-table"><tr><td>';

        echo '<strong>Enter A Short Description:</strong>
            <input type="text" name="_desc" id="short-desc" value="'.$meta['_desc'].'" size="30" />';

    echo '</td></tr></table>'; // end table
} 

/** Save the Data **/
function save_custom_meta($post) {
    // verify nonce
    if (!wp_verify_nonce($_POST['shortdesc_meta_box_nonce'], basename(__FILE__))) 
        return $post->ID;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post->ID;
    // check permissions
    if (!current_user_can('edit_post', $post->ID))
        return $post->ID;

    //echo $_POST['_desc'];
    //die();

    if(isset($_POST['_desc']))
        update_post_meta($post->ID, '_desc', strip_tags($_POST['_desc']));
}
add_action('save_post', 'save_custom_meta', 1, 2);
share|improve this question
    
What value are you trying to store? In case you do get this working later, the way you have this setup, no one will be able to remove post meta. You should add an else for if isset($_POST['desc']) is not true... to delete_post_meta –  GhostToast Aug 26 '13 at 17:07
add comment

1 Answer 1

up vote 1 down vote accepted

This is because save_post hook function accepts post id as parameter and not post object so the save_custom_meta function should be as following.

/** Save the Data **/
function save_custom_meta($post_id) {
    // verify nonce
    if (!wp_verify_nonce($_POST['shortdesc_meta_box_nonce'], basename(__FILE__))) 
    return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return $post_id;
    // check permissions
    if (!current_user_can('edit_post', $post_id))
    return $post_id;

    //echo $_POST['_desc'];
    //die();

    if(isset($_POST['_desc']))
        update_post_meta($post_id, '_desc', strip_tags($_POST['_desc']));
}
add_action('save_post', 'save_custom_meta', 1, 2);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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