Leverage Functions
You can extract the redundant code into reusable functions. For example, the last two functions could be rewritten like this. My resulting code is actually 2 lines longer, but it’s much cleaner from a debugging standpoint. And, if there were a third custom field, this approach would definitely be shorter.
function slidestoshow_save_post_meta_box( $post_id, $post ) {
$nonce="slidestoshow_page_meta_box_nonce";
$meta_label="Slides to Show Scroll";
$meta_slug = 'slides-to-show-scroll';
if ( !my_save_post_met_box( $post_id, $post, $meta_label, $meta_slug, $nonce ) ) {
return $post_id;
}
}
function slidetype_save_post_meta_box( $post_id, $post ) {
$nonce="slidetype_page_meta_box_nonce";
$meta_label="Slide Type";
$meta_slug = 'slide-type';
if ( !my_save_post_met_box( $post_id, $post, $meta_label, $meta_slug, $nonce ) ) {
return $post_id;
}
}
function my_save_post_meta_box( $post_id, $post, $meta_label, $meta_slug, $nonce ) {
if ( !wp_verify_nonce( $_POST[$nonce], plugin_basename( __FILE__ ) ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$meta_value = get_post_meta( $post_id, $meta_label, true );
$new_meta_value = stripslashes( $_POST[$meta_slug] );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_label, $new_meta_value, true );
elseif ( $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_label, $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_label, $meta_value );
}
And, you could reclaim your two lines by renaming your nonce to the same scheme as your slug. Something like this:
if ( !wp_verify_nonce( $_POST[$meta_slug . '_page_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
Use Arrays and Foreach Loops
Your action hooks could be optimized like this. Again, more lines of code–with only two custom fields–but easier to read and debug.
$fnc_labels = array( 'slidestoshow', 'slidetype' );
foreach ( $fnc_labels AS $fnc_label ) {
add_action( 'admin_menu', $fnc_label . '_create_post_meta_box' );
add_action( 'save_post', $fnc_label . '_save_post_meta_box', 10, 2 );
}