Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Can you explain why you want to do this? What it the result you want? It can effect the answer. –  s_ha_dum Dec 10 '12 at 15:19
    
@s_ha_dum I plan to explode the post title and add them as post tags, thanks for dropping by –  ded syd Dec 10 '12 at 15:48
    
After your edits I have to ask: have you read How to add terms to a post depending on its title? –  toscho Dec 10 '12 at 17:13
add comment

1 Answer

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;
}
share|improve this answer
    
thanks @toscho for the fast answer, but is there a way to catch the title before it's saved in the database without changing it? –  ded syd Dec 10 '12 at 16:23
    
You can use almost the same code. Just save (copy) $post_data['post_title'] somewhere and return the $post_data without changing it. –  toscho Dec 10 '12 at 16:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.