How to intercept Post Title on Post-Save

I am new to WP and I couldn’t find this solution by myself.

this is my function,

function explodetitletotags() {

    global $post;

    $title = $post->post_title;

    $tags = explode(' ', preg_replace('/[^\p{L}0-9 ]/', ' ', $title)); 

    foreach $tags as $tag {

        global $post;

        $thePostID = $post->ID;

        wp_add_post_tags($thePostID, $tag);

    }

}

add_action('publish_post', 'explodetitletotags');

The Function works like a charm except When the post title in the new single post page updated, the title always give the previous value, not the value the last time I typed in the input title box.

My assumption here is, $title = $post->post_title; in my function call the old version of the title from Database instead of the new version, I think why don’t just grab the title before it sent to the Database so the title always the latest version, is this okay?

If yes, Is there a way to intercept/catch wordpress on Post-Save before it’s saved in wp_posts table Database?

I am sorry for my English, since it’s not my first tongue and Thank You for your help.

1 Answer
1

To set the title before it is saved, hook into wp_insert_post_data:

add_filter( 'wp_insert_post_data', 'wpse_75597_change_title' );

function wpse_75597_change_title( $post_data )
{
    $post_data['post_title'] = 'SOMETHING VERY LOUD';

    return $post_data;
}

Leave a Comment