How to Edit the Title When a Custom Post Type Saves in WordPress
Custom post types offer immense flexibility in organizing your WordPress content. But what if you need to manipulate the title of a custom post type upon saving? You might want to:
- Add a prefix or suffix to the title. Imagine you're managing a blog with recipes and want each recipe title to start with "Recipe:".
- Capitalize the first letter of each word. You might desire all your product titles to follow proper capitalization rules.
- Extract specific information from the post content. Perhaps you want to use a specific keyword from the post's body to modify the title.
No matter your specific need, WordPress provides ways to achieve this with a bit of code.
Understanding the Process
The core of the solution involves using the save_post
action hook in WordPress. This hook triggers a function whenever a post is saved. Inside this function, you'll grab the post title, apply your desired modifications, and then update the post data.
The Code Example
Here's a basic example of how you can automatically add a prefix to the title of a custom post type called "product":
post_type === 'product' && $post->post_status !== 'auto-draft' ) {
// Get the current title
$original_title = $post->post_title;
// Add the prefix
$new_title = 'Product: ' . $original_title;
// Update the post title
wp_update_post( array(
'ID' => $post_id,
'post_title' => $new_title,
) );
}
}
Breakdown of the Code
-
add_action( 'save_post', 'update_product_title', 10, 2 )
: This line hooks the functionupdate_product_title
to thesave_post
action. The third argument (10) represents the priority of the hook, and the fourth argument (2) indicates that the function accepts two parameters: the post ID and the post object. -
function update_product_title( $post_id, $post )
: This defines the function that will execute on post save. -
if ( $post->post_type === 'product' && $post->post_status !== 'auto-draft' )
: This conditional statement checks if the post type is 'product' and if the post is not an auto-draft. This ensures the code only runs for the correct post type and when a new post is actually being saved. -
$original_title = $post->post_title
: This line retrieves the original title of the post. -
$new_title = 'Product: ' . $original_title
: This line constructs the new title by adding the desired prefix ("Product: ") to the original title. -
wp_update_post( array( ... ) )
: This function updates the post data in the database. The array contains the post ID and the new title.
Further Enhancements
- Regular Expressions: For more complex manipulations like capitalization or extracting specific information from the content, you can utilize regular expressions within the
update_product_title
function. - Filtering Title: Instead of directly updating the post title, you can use the
pre_post_title
filter. This allows you to modify the title before it's saved to the database.
Conclusion
Editing the title when a custom post type saves empowers you to tailor your content organization to your unique needs. This technique opens doors to automating title formatting, injecting valuable data, and creating a consistent experience for your website users. Remember to always test your code thoroughly before implementing it on a live website.