WordPress

Wp_title In WordPress 4.4

Recently I wrote an article about what is coming in the next version of WordPress and one of those was the changes to do with the wp_title() function. There seems to be some confusion about what I was trying to say about the deprecation call so I'll try to explain this a bit better.

Previously the way you would handle title tag generation in WordPress is to use the wp_title function.

<html>
<head>
<title><?php wp_title(); ?></title>

<?php wp_head(); ?>
</head>

In the WordPress core you can find this function inside wp-includes/general-template.php to see exactly how it works. But it will basically work out what page/post type it is on by using conditional tags such as is_single(), is_category(), is_author() etc. Depending on the type of post it will display the content inside the title tag of your page.

If you look towards the bottom of the function you will see a filter is applied which will allow plugin developers to change the title tag if you want something different to the default behaviour.

WordPress 4.1

As of version 4.1 a feature was added to move the title tag creation from the wp_title() and into a theme support setup. This means in the functions.php file you can add the following code and use the add_theme_support('title-tag') function.

function setup_theme() 
{
   add_theme_support('title-tag');
}

add_action( 'after_setup_theme', 'setup_theme' );

By adding this line you are telling WordPress that you are no longer going to be outputting the title tag in your head tag and you allow WordPress to do this for use by outputting the title tag as part of the wp_head() output, therefore you can change your head tag to the following.

<html>
<head>
<?php wp_head(); ?>
</head>

WordPress 4.4

Because the wp_title() function was not unused by themes because of the change in 4.1 the wp_title() function could therefore be deprecated from the WordPress core, this is what was purposed for version 4.4.

Therefore the wp_title() was classed as deprecated and was moved into the wp-includes/deprecated.php file which is home to all the old deprecated functions in WordPress. This means that you can still use the function but a deprecated notice is thrown and it is recommended that you don't use the function anymore.

As of 12th November the wp_title() function was reinstated into the WordPress wp-includes/general-template.php file.

UPDATE 12 November – wp_title has been reinstated until alternative usages have been identified and a path forward for them defined.

Although it has been reinstated into the core code it is still recommended that you don't use the function, as WordPress are continuing to look for a solution to the wp_title() function and therefore will be removing this in the future so it's best to get your themes ready for that now.

With the new update in 4.4 it comes with a few new filters that you can use to customise the output of the title text.

  • pre_get_document_title - Will allow you to have full control of the title tag, if this filter does not return empty then it will be returned as the title text.
  • document_title_separator - By default the title separator is - but you can customise this by using this filter. For example if you prefer to use a | then you would change this by using this filter.
  • document_title_parts - This allows you to customise the different parts which make up title tag these are: title, page number, tagline and site.

pre_get_document_title

The output of this filter will change all title tags in site, you can use this to a number of ways.

You could return the samething on every page (very bad for user experience and SEO).

function same_title_tag()
{
    return 'Paulund';
}
add_filter('pre_get_document_title', 'same_title_tag');

Every title tag on the site will now say Paulund.

Or you could use a post meta item to manually change the title tag to anything you want.

function post_meta_title_tag()
{
    $customPostTitleMeta = get_post_meta( get_the_ID(), 'custom_post_title', true);
    if($customPostTitleMeta)
    {
         return $customPostTitleMeta;
    }

    return '';
}
add_filter('pre_get_document_title', 'post_meta_title_tag');

document_title_separator

Used to change the separator in the title, to change from - to | use the following function.

function change_separator()
{
    return '|';
}
add_filter('document_title_separator', 'change_separator');

document_title_parts

The tagline is one of the options in the title array passed into this filter, the tagline is the site description which is added on the home page, I can see this filter being commonly used to remove the tagline from the title pages.

function remove_tagline($title)
{
    if(isset($title['tagline']))
    {
        unset($title['tagline']);
    }

    return $title;
}
add_filter('document_title_parts', 'remove_tagline');

For further information about the WordPress title generation please view the development notes Document Title in 4.4.

Back to top

Fastest WordPress Hosting With WPEngine

Stunning speed, powerful security, and best-in-class customer service. At WP Engine.

Risk free for 60 days

Leave a Reply

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