save_post
is called when post is inserted or updated. When you access add new post a post is created and inserted into database. That is actually draft. At that time $_POST
is blank therefore, you are seeing warnings.
Solution:
The best option is exit the function as soon as you don’t find the nonce
. And use isset()
to check if key is present.
Example:
function save_slider_img( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if (!isset($_POST['meta_slider_noncename'])) {
return;
}
if(!wp_verify_nonce($_POST['meta_slider_noncename'], plugins_url(__FILE__))) {
return;
}
if ('slider' == $_POST['post_type'] && current_user_can( 'edit_page', $post_id )) {
$images = isset($_POST['meta-slider_img']) ? $_POST['meta-slider_img'] : false;
update_post_meta( $post_id, '_meta-slider_img', $images );
}
}