Post save functions are conflicting with each other when adding them to the save_post action hook.
2 different custom post types with 2 different (one for each post type) custom meta boxes.
I'm only including the code for 1 of the meta boxes. The other one is very similar and each one works fine separately but not together.
The 'register_metabox_cb' call back function:
function c3m_video_meta() {
add_meta_box('_c3m_video_embed', __('Enter Video Embed Code In the Box Below') , 'c3m_video_embed', 'video', 'normal', 'low');
}
Adding the meta box to the post edit screen:
function c3m_video_embed($post) {
global $post;
wp_nonce_field(__FILE__,'video_nonce');
$embed-code = get_post_meta($post->ID, '_embed-code', true);
echo '<input type="text" name="_embed-code" value=""' . $embed-code . '" class="widefat" />' ;
}
The save function:
function c3m_save_video_meta( $post_id , $post ) {
if ( !wp_verify_nonce( $_POST [ 'video_nonce' ], __FILE__ ) ) { return $post ->ID;
}
if ( !current_user_can( 'edit_post' , $post ->ID )) return $post ->ID;
$c3m_video_embed-code [ '_embed-code' ] = $_POST [ '_embed-code' ];
foreach ( $c3m_video_embed-code as $key => $value ) {
if ( $post ->post_type == 'revision' ) return ;
$value = implode( ',' , ( array ) $value );
if (get_post_meta( $post ->ID, $key , FALSE)) {
update_post_meta( $post ->ID, $key , $value ); } else {
add_post_meta( $post ->ID, $key , $value ); } if (! $value ) delete_post_meta( $post ->ID, $key );
}
}
The action hooks:
add_action( 'admin_menu' , 'c3m_video_meta' );
add_action( 'save_post' , 'c3m_save_video_meta' , 1, 2);
This is fine and dandy and works but when I add another meta box to a different post type and use a similar function (different function name, nonce name and key ) when the post is saved I get this error:
Notice: Undefined index: _embed-code in /Applications/MAMP/htdocs/wordpress/wp-content/plugins/ieew-custom-functions/ieew-custom-functions.php on line 181
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/wordpress/wp-content/plugins/ieew-custom-functions/ieew-custom-functions.php:181) in /Applications/MAMP/htdocs/wordpress/wp-includes/pluggable.php on line 897
The undefined _embed-code is when saving the post that does not contain the _embed-code variable.
Since the error messages are reversed depending on wich post type I try to save it leads me to believe that both (2 different) save functions are being added to the save_post action. They are also being added when saving a normal post. If I'm only using 1 of the save functions there are no errors when saving a normal post.
Rather than this question being a "Fix My Code" question I would rather the answer contain the how and why of adding custom meta boxes and using the various nonce methods. Sure I could use the More Fields plugin but I would rather learn the best way to accomplish customizing the post edit screen with custom content types.
I have used this same code and method to add multiple meta boxes to a single custom post type and it has always worked fine.