If you’ve submitted a theme to the Review Unit recently, you may have gotten this feedback:

Themes are REQUIRED to use ‘wp_title’ filter, to filter wp_title() (RECOMMENDED), or pass argument to wp_title() (OPTIONALLY), in order to modify document title content.

At first I was like “um wat…” but then I realized it’s a much cleaner way to echo the title tag since it allows plugins or other code to get an equal bite at fully changing the page title (no hardcoded title parts in themes).

However, unlike most other WordPress theme topics, there is a real dearth of practical examples or even explanations on the internet about how to actually do this for some odd reason. The codex page is quite unhelpful and I Googled 10 pages deep and only found 1 passing mention. So I had to figure this out on my own the old-fashioned, pre-internet way: trial and error.


This is the acceptable solution I came up with. It will add in the site title, index page type (if there is one) and the page/multi-page number. In your theme’s functions.php file, add the following function (remember to change both yourthemeslug_’s to a unique slug string for your theme):

 * Title tag filter 
function yourthemeslug_title_filter( $title, $sep, $seplocation ) {
	// get special index page type (if any)
	if( is_category() ) $type = 'Category';
	elseif( is_tag() ) $type = 'Tag';
	elseif( is_author() ) $type . 'Author';
	elseif( is_date() || is_archive() ) $type = 'Archives';
	else $type = false;

	// get the page number
	if( get_query_var( 'paged' ) ) 
		$page_num = get_query_var( 'paged' ); // on index
	elseif( get_query_var( 'page' ) ) 
		$page_num = get_query_var( 'page' ); // on single
	else $page_num = false;

	// strip title separator
	$title = trim( str_replace( $sep, '', $title ) );
	// determine order based on seplocation
	$parts = array( get_bloginfo( 'name' ), $type, $title, $page_num );
	if( $seplocation == 'left' ) 
		$parts = array_reverse( $parts );

	// strip blanks, implode, and return title tag
	$parts = array_filter( $parts );
	return implode( ' ' . $sep . ' ', $parts );

Then add the following alongside the rest of your hooks, actions and filters in your functions.php (you can put it anywhere, but I group them together at the bottom… so they get lonely):

// call our custom wp_title filter, with normal (10) priority, and 3 args
add_filter( 'wp_title', 'yourthemeslug_filter_wp_title', 10, 3 );

Now, in the header.php file you can call wp_title() by itself and get the filtered title instead. If you include an optional separator when you call wp_title(), it will get passed to the filter function and incorporated uniformly (if you don’t stipulate a $sep argument, WordPress will just use «):


So let’s say we we’re on the third page of the index for the category named “Bats.” Without our filter function the title when we call wp_title() would be “Bats“, not terribly descriptive. But with our filter function, the title when we call wp_title() would be “The Blog Name » Category » Bats » page 3“, much better.

Hopefully no one else will have to fumble around with this issue now. Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation