Change post title during post saving process

My theme and Qtranslate plugin have a conflict and cause many issues on my website’s post titles. I cannot trace what causes the issue and I thought that I could handle the title saving myself instead.

I would like to be able to change post title that wordpress uses when saving a post from backend.

Read More

How can I do that?

Related posts

Leave a Reply

1 comment

  1. You can hook into wp_insert_post_data and change things.

    <?php
    add_filter('wp_insert_post_data', 'wpse67262_change_title');
    function wpse67262_change_title($data)
    {
        $data['post_title'] = 'This will change the title';
        return $data;
    }
    

    Obviously you’re going to have to some checks and stuff so you don’t change every post title. That hook will also fire every time the post is saved (adding a new post, updating, trashing a post, etc) and for every post type. $data is an associative array with every column you’d find in the {$wpdb->prefix}_posts table as keys. So if you just wanted to changed posts:

    <?php
    add_filter('wp_insert_post_data', 'wpse67262_change_title');
    function wpse67262_change_title($data)
    {
        if('post' != $data['post_type'])
            return $data;
    
        $data['post_title'] = 'This will change the title';
    
        return $data;
    }