0

I've had multiple errors with this metabox, first I thought Id fixed a undefined index error with the code below but now that doesn't appear my metabox doesn't save any data.

code before the error:

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

I tried adding the following to fix the undefined index error on line 108

 if ( !isset( $POST['excerpt_meta_box_nonce'] ) || !wp_verify_nonce($_POST['excerpt_meta_box_nonce'], basename(__FILE__)))   
        return $post_id; 

after adding this now any data I put in the metabox doesn't save.

any help would be appreciated

code for excerpt meta box

 <?php
// Add the Meta Box  
function vs_add_excerpt_meta_box() {  
    add_meta_box(  
        'excerpt_meta_box', // $id  
        'Excerpt Meta Box', // $title   
        'vs_show_excerpt_meta_box', // $callback  
        'post', // $page  
        'normal', // $context  
        'high'); // $priority  
}  
add_action('add_meta_boxes', 'vs_add_excerpt_meta_box');  

// Field Array  
$prefix = 'custom_';  
$excerpt_meta_fields = array(  
    array(
        'label' => 'Custom post teaser',
        'desc'  => 'Replace the default excerpt with the text of your choice',  
        'id'    => $prefix.'excerpt',  
        'type'  => 'textarea' 
        )
);  

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

    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($excerpt_meta_fields as $field) {  
        // get value of this field if it exists for this post  
        $meta = get_post_meta($post->ID, $field['id'], true);  
        // begin a table row with  
        echo '<tr> 
                <th><label for="'.$field['id'].'">'.$field['label'].'</label></th> 
                <td>';  
                switch($field['type']) {  
                    // case items will go here   
            // textarea  
            case 'textarea':  
                echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> 
                    <br /><span class="description">'.$field['desc'].'</span>';  
            break; 

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

// Save the Data  
function vs_save_excerpt_meta($post_id) {  
    global $excerpt_meta_fields;  

    // verify nonce  
    if ( !isset( $_POST['excerpt_meta_box_nonce'] ) || !wp_verify_nonce($_POST['excerpt_meta_box_nonce'], basename(__FILE__)))   
        return $post_id;  
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;  
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  

    // loop through fields and save the data  
    foreach ($excerpt_meta_fields as $field) {  
        $old = get_post_meta($post_id, $field['id'], true);  
        $new = $_POST[$field['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'vs_save_custom_meta');    
?>
6
  • It is $_POST, not $POST
    – s_ha_dum
    Jun 29, 2013 at 19:01
  • Changed that s_ha_dum and it still doesn't display anything in the field After I update it.
    – Dannyw24
    Jun 29, 2013 at 19:47
  • I was hoping it was a simple typo :)
    – s_ha_dum
    Jun 29, 2013 at 19:56
  • So was I would of been very nice for us all :) I'm sure its a typo of some sort though sure of it.
    – Dannyw24
    Jun 29, 2013 at 20:02
  • 1
    You seem to be having a lot of trouble with metaboxes. Have you given any thought to using something like wordpress.org/plugins/meta-box or farinspace.com/wpalchemy-metabox/ or even wordpress.org/plugins/types to handle the more complicated aspects for you? Jun 30, 2013 at 13:45

1 Answer 1

2

Ok, you do have a typo :

add_action('save_post', 'vs_save_custom_meta');   

should be :

add_action('save_post', 'vs_save_excerpt_meta');   

So you meta box does not have a valid callback. Change this and it will work ^^

1
  • You are great jma! thanks for that I was looking so hard at my code I didn't see the simpleness of this fault :O
    – Dannyw24
    Jul 3, 2013 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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