WP action/filter to modify title before header output and article output?

I’m trying to split test two versions of a posts title using a custom plugin. So far I haven’t found the right filter or action to hook into to modify the title on the meta tag level before the page is output.

What I have found is the filter the_title but that looks to do it in the post content only, and not he document head.


Is this the right filter to use, and I just have to modify my theme template to accomplish the selected action? Can I do this strictly from a plugin without theme modification?

What I want to do is check if a GET variable is set, such as ?title_ver=1 and if it is set, then grab the appropriate post meta value for the alternative title.

Non working code

    function custom_modify_title( $title, $id = null ) {
        return 'CUSTOM '.$title;
    add_filter( 'wp_title', 'custom_modify_title', 10, 2 );
    add_filter( 'the_title', 'custom_modify_title', 10, 2 );

Solutions Collecting From Web of "WP action/filter to modify title before header output and article output?"

The filter wp_title changes only the function wp_title() when it’s called.

So, you must double check on your theme’s source code if before your header is using wp_title.

Anyway, you can set your filter on functions.php. A function to identify your param GET should be something like so:

function maybe_change_wp_title_ver( $title, $sep ) {

    if ( isset( $_GET['title_ver'] ) && 1 === $_GET['title_ver'] ) {
        $title = 'My New Title';

    return $title;

add_filter( 'wp_title', 'maybe_change_wp_title_ver', 99, 2 ); //99 is set as priority (read comments)