0
votes

I'm trying to create a custom meta box to save a short post summary for a theme.

I've created the metabox which displays as expected on the post admin page. When the post is saved, the text from the meta box is not being saved. I've looked at the wp_postmeta table and see no additions.

Can anyone shed any light on this for me?

Thanks

function smry_custom_meta(){

    $postTypes = array('post', 'portfolio');

    foreach ($postTypes as $postType) {
        add_meta_box(
            'summary-meta-box', // id
            __('Post Summary'), // title
            'smry_show_meta_box', // callback
            $postType, // post type
            'normal' // position
            );
    }
}
add_action('add_meta_boxes', 'smry_custom_meta');

function smry_show_meta_box(){

    global $post;
    $meta = get_post_meta($post->ID, 'smry_text', true);

    <p>
        <label>Summary Text</label>
        <textarea name="smry_text" id="smry_text" cols="60" rows="5">
            <?php echo $meta; ?>
        </textarea>
    </p>
    <?php
}

function save_summary_meta(){

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

    $new = $_POST["smry_text"];

    update_post_meta($post_id, 'smry_text', $new);
}
add_action('save_post', 'save_summary_meta');
1
Inside save_summary_meta(), you use $post_id. Where are you getting this variable from? - henrywright
Thats it. I'd forgotten to pass it into the function. Thanks for pointing it out :-) - tonyedwardspz

1 Answers

0
votes

I think your problem could use your use of $post_id inside save_summary_meta(). Try this instead:

function save_summary_meta(){
    global $post;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
        return $post->ID;
    }

    $new = $_POST["smry_text"];

    update_post_meta($post->ID, 'smry_text', $new);
}
add_action('save_post', 'save_summary_meta');