I would suggest the following improvements for better code readability and to make it easier to maintain later.
-
Assuming you’ve removed title from your custom post type using the following function :
remove_post_type_support( 'slider', 'title' )
Now if you use
name="post_title"
instead ofname="wys_slider_title"
WordPress will still use it and update the post title accordingly. Hence you don’t have to worry about the title and you can focus on your custom fields. This results in a much better code and solves your problem of duplicating the title and then saving it.
*This helps you avoid usingwp_insert_post_data
additional filter.
*And since you’re not manually updating the title usingwp_update_post
function, you don’t have to worry about thesave_post
infinite loop. -
Before you save any data, you want to make sure there’s nothing malicious in there. Fortunately, WordPress provides a bunch of functions for Data Validation
// Use: update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) ); // Instead of: update_post_meta( $post_id, 'my_meta_box_text', $_POST['my_meta_box_text'] );
-
Use
save_post_{$post_type}
instead of plainsave_post
, This helps you avoid unnecesaryIF
statements
e.g. In your case,
add_action( 'save_post_slider', 'wys_slides_save_details' );
you won’t need to wrap your logic around an
IF
statementif( $post_type == 'slider' ) {...CODE...}
Since
save_post_slider
will only run forSlider
post type.