0

I'm getting the following error...

Notice: Undefined index: album_tracks_metabox_nonce in /Applications/MAMP/htdocs/TheToasters/wp-content/themes/Toasters/discography-post-type.php on line 124

...whenever I attempt to add a new Page, Post or custom post type entry. If I'm editing an existing entry, all works fine.

Error screenshot

The error appears to be relating to a nonce for a metabox on the "Discography" custom post type I've created, here's the relevant code:

function album_tracks_content( $post ) {
    wp_nonce_field( 'album_tracks_metabox', 'album_tracks_metabox_nonce' );
    $albumTracks = get_post_meta( get_the_ID(), 'album_tracks', true);?>
    <p>1. <input type="text" id="track1" name="track1" placeholder="Track name" value="<?php echo isset($albumTracks[0]) ? $albumTracks[0] : ''; ?>"></p>
    <p>2. <input type="text" id="track2" name="track2" placeholder="Track name" value="<?php echo isset($albumTracks[1]) ? $albumTracks[1] : ''; ?>"></p>
<?php }
// Save Tracks metabox
function album_tracks_save( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    return;

    if ( !wp_verify_nonce( $_POST['album_tracks_metabox_nonce'], 'album_tracks_metabox' ) )
    return;

    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
        return;
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
        return;
    }

    $albumTracks = array();
    if (isset($_POST['track1'])):
        $albumTracks[] = $_POST['track1'];
    endif;
    if (isset($_POST['track2'])):
        $albumTracks[] = $_POST['track2'];
    endif;
    if (!empty($albumTracks)):
        update_post_meta( $post_id, 'album_tracks', $albumTracks );
    endif;
}
add_action( 'save_post', 'album_tracks_save' );

?>

The exact line 124 is this:

if ( !wp_verify_nonce( $_POST['album_tracks_metabox_nonce'], 'album_tracks_metabox' ) )

1

Whoever wrote your theme didn't bother to verify the existence of array keys before using them. The error is happening because the key album_tracks_metabox_nonce doesn't exist in the $_POST array.

The line likely should be:

if (
  !isset($_POST['album_tracks_metabox_nonce']) ||
  !wp_verify_nonce($_POST['album_tracks_metabox_nonce'], 'album_tracks_metabox')
) {
  // whatever is in the if condition, likely `return`
}
  • That would be me, however this is not a theme for production/sale, just getting to grips with the Wordpress system (creating post types, taxonomys, metaboxes, and a long etc.). The code used was based on what I'd seen at codex.wordpress.org/Function_Reference/add_meta_box in the examples. Where they used if ( ! wp_verify_nonce($_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) { return; }. Non the less, your solution seems to have fixed the error - cheers! – Brad Adams Jan 11 '15 at 20:25
  • Arg, yes, right above the code I took is if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) { return;} (basically what you've added... my bad) – Brad Adams Jan 11 '15 at 20:34

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.