Filtering wp_title() for Better SEO
by c.bavota | Posted in Tutorials | 15 comments
When I last submitted Magazine Basic to the WordPress theme directory, I was told that they no longer allowed direct modification of certain elements within the header.php file. That meant I could no longer include a couple of conditional statements around the wp_title() function to improve on its SEO. The new approach in the theme review guidelines is to filter the function, which is actually a more efficient way of doing it. Now all you have to do in your header.php file in include the wp_title() function, the rest of the magic happens in your functions.php file.
add_filter( 'wp_title', 'filter_wp_title' );
/**
* Filters the page title appropriately depending on the current page
*
* This function is attached to the 'wp_title' fiilter hook.
*
* @uses get_bloginfo()
* @uses is_home()
* @uses is_front_page()
*/
function filter_wp_title( $title ) {
global $page, $paged;
if ( is_feed() )
return $title;
$site_description = get_bloginfo( 'description' );
$filtered_title = $title . get_bloginfo( 'name' );
$filtered_title .= ( ! empty( $site_description ) && ( is_home() || is_front_page() ) ) ? ' | ' . $site_description: '';
$filtered_title .= ( 2 <= $paged || 2 <= $page ) ? ' | ' . sprintf( __( 'Page %s' ), max( $paged, $page ) ) : '';
return $filtered_title;
}
The above function will change the page title according to whichever page is currently being viewed. On your homepage, you’ll see your site name and site description. All other pages/posts/categories will have their name first and then the site name. This helps improve your site’s SEO by giving each page a unique title and is the first step in preparing your site for proper indexing by every popular search engine.
In order to make everything work correctly, make sure that the title tag in your header.php file looks like this:
<title><?php wp_title( '|', true, 'right' ); ?></title>



It needs a space in the first line, because otherwise, the title and name get crammed together, making them both unreadable.
It should look like this:
“$title . ‘ ‘ . get_bloginfo( ‘name’ );”
Definitely
Good solution..
Just make sure that your title tag appears like the second snippet above and you will not require that extra space.
Dude, is this only for your theme Magazine Basic or it will be good for all general themes? if I use All in One SEO plugin, should I add this code to my header.php or it’s not necessary? Thanks in Advance
If you’re using an SEO plugin then you don’t need to add this code since the plugin will most likely do something similar.
WOW!! This is really awesome! Just what i was looking for. Thank you so much for writing this post.
Hi bavota, thanks for your post. I am already use seo plugin on my Wp site. If i use these code, is it necessary to use all in one seo plugin? please tell me. Thank you
This code snippet is an alternative to having to use an SEO plugin for unique page titles.
You code is solution for duplicate title in pagination, thanks bavota
Thank you for amazing information, I just start looking for seo for my site
Hi, bavota. I’m looking for seo tutorial for wordpress. Your blog give me a bunch of knowledge. Thanks for sharing
Is there an update to this? The title on feed/rss page is doubled.
Thanks for reminding me. I had been meaning to update this one. Just added the check to see if it is a feed so that the default title is returned. Check out the code again.
I don’t get it why my title is doubled on every page ?
Is there a plugin installed that could be conflicting with this code?