Skip to content
Advertisement

Append post title before publishing the wordpress post

I want to append my wordpress titles to have some static text in them before publishing. As my posts are directly posting to social media when they are published, I want this to happen before publishing.. I tried several approaches but none of them are working. This does not work at all

function save_title( $title ){
        $text = '#ad #Myblog';
    $title = $title.$text;
}
add_action('save_post', 'save_title');

Below code works but just replace the whole title with static text and do not capture text I entered in post title manually when writing the post

function update_new_post_title( $data ) {
   $title = '#ad '.$_POST[ 'title' ];
    $data['post_title'] =  $title;
    return $data;
}
add_filter( 'wp_insert_post_data' , 'update_new_post_title' , '99', 1 );

I also tried to_title but that just update the title in wordpress. Not in the post before publishing. Not sure if there is really a way to capture dynamic text entered in post and append. Please help.

Advertisement

Answer

Check that https://wordpress.stackexchange.com/a/35993

add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );

function filter_post_data( $data , $postarr ) {
    // Change post title
    if (!strpos($data['post_title'], '_suffix')
        $data['post_title'] .= '_suffix';
    return $data;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement