1

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.

http://codex.wordpress.org/Plugin_API/Filter_Reference/the_title

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 );
1

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)
  • Thanks. I'll double check the theme. I have used that filter but it only modified the output on the content, not in the title tag. I'll report back. – user3657553 Feb 10 '15 at 22:36
  • it's using wp_title(''); I'll see if I can hook in to that. – user3657553 Feb 10 '15 at 22:37
  • I'm using your code, but with wp_title as the hook, no luck yet. I'll see what the issue is. – user3657553 Feb 10 '15 at 22:39
  • If you paste my code on your functions.php, you should be able to see: My New Title instead of your titles when you refer ?title_ver=1 – Leo Caseiro Feb 10 '15 at 22:40
  • I've updated my original post with my sample code. It's in a custom plugin I'm creating, and I'd like to use it here before modifying the theme. So that I can release this abroad and not worry about config issues. – user3657553 Feb 10 '15 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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