Want to use save_post to replace the post’s title in WordPress? Use following code in your theme’s functions.php file OR in site specific plugin.
This simplest method would be to edit the data at the point it’s inserted, rather than updating it afterwards, using wp_insert_post_data instead of save_post. This works on creating a new post or updating an existing post without change. It also avoids the danger of creating an infinite loop by triggering update_post within save_post.
1 2 3 4 5 6 7 8 9 10 11 |
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 ); // Grabs the inserted post data so you can modify it. function modify_post_title( $data ) { if($data['post_type'] == 'rating' && isset($_POST['rating_date'])) { // If the actual field name of the rating date is different, you'll have to update this. $date = date('l, d.m.Y', strtotime($_POST['rating_date'])); $title = 'TV ratings for ' . $date; $data['post_title'] = $title ; //Updates the post title to your new title. } return $data; // Returns the modified data. } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.