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.
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' ) )