Custom document titles in WordPress 4.4, Genesis, and WP SEO

Adding the global site title to wp_title

As part of a massive WordPress multisite installation, I wanted to force rewrite the document titles of all pages globally on a custom Genesis theme. Genesis includes its own document title filter, all managed through the dashboard of each site. On my installation child site document titles were missing the name of the global parent site.  However, manually entering custom titles via WordPress SEO or Genesis SEO settings would have taken forever on . So my solution was just to add a filter to wp_title() and add the global title where needed. Here’s what I came up with:

/**
* Remove site specific Genesis SEO title settings and creates template for titles globally
*/

 //Stop Genesis from using the SEO settings from dashboard
remove_filter( 'wp_title', 'genesis_default_title', 10, 3 ); 

//Filter default wordpress title to append SAU where needed
function sau_wp_title( $title, $sep ) {
global $blog_id;
$global_details = get_blog_details( 1 );
$global_title = $global_details->blogname;
$sep = ' | '; 
	
if ( ($blog_id != 1) && ( !is_home()) && (!is_front_page() ) ) {
$title = "$title $sep $global_title";
} // end if
return $title;
} 

add_filter( 'wp_title', 'sau_wp_title', 10, 2 );

It worked great… until a week later when I upgraded WordPress to 4.4. The new release replaced wp_title with an entirely new method for theme and plugin developers to customize document titles.

Plugin authors can now check for theme support and have a few new filters available that will allow them to change or replace the title in a reliable way:

  • 'pre_get_document_title' short-circuits wp_get_document_title() if it returns anything other than an empty value.
  • 'document_title_separator' filters the separator between title parts.
  • 'document_title_parts' filters the parts that make up the document title, passed in an associative array.

WordPress 4.4 adds document title filters

Googling around, I found a post that explain these new filters at length and rewrote my snippet to rewrite ALL titles globally just to my liking. Note: I did not change the snippet below to work for any site, as the $tagline variable will need to be rewritten to pull from get_bloginfo.

add_filter('pre_get_document_title', 'sau_pre_get_document_title', 10);
function sau_pre_get_document_title(){
 global $blog_id;
 global $post;
 $blog_title = get_bloginfo();
 $page_title = get_the_title($post->ID);
 $sep = ' | '; 
 $global_details = get_blog_details( 1 );
 $global_title = $global_details->blogname;
 $tagline = 'Modern, Affordable, Competitive';

 if ( ($blog_id == 1) ) {
    if (is_home()) {
    $title = "$global_title $sep $tagline";
    return $title;
    }
 
else {
    $title = "$page_title $sep $global_title";
    return $title;
    }
}

if ( ($blog_id != 1) && ( is_home()) || (is_front_page() ) ) {
    $title = "$blog_title $sep $global_title";
    return $title;
}

if ( ($blog_id != 1) && ( !is_home()) && (!is_front_page() ) ) {
   $title = "$page_title $sep $blog_title $sep $global_title";
   return $title;
}
 
else {
   return $title;
}
}

Removing Yoast WP SEO titles

For those that are using Yoast’s WP SEO and want to stop it from forcing itself upon your custom title set up, all you have to do is add the following to your code (renaming the function added to the filter as needed):

add_filter('wpseo_title', 'sau_pre_get_document_title', 15);