Upgrade WordPress Themes for depreciated wp_title()

By Karishma Sibal | October 28, 2015

Whenever one opens the header.php of any of the old theme (except the “twenty” themes) then one will find the below code for it:

<title><?php wp_title(‘|’, true, ‘right’); ?><?php bloginfo(‘name’); ?></title>

The code will basically output the below things for the WordPress Page or Post:

  • Title of WordPress Page/Post or any other page (archive/search)
  • Actual Title of the Page generated
  • Date
  • Category of the Page (if available)
  • Author of the Page (if available)
  • Name of the blog

Each of this field is separated by a separator “|” 

Problems faced due to this function:

  • It becomes difficult for the SEO plugin authors to generate as well as include the SEO title in the document’s head as the theme is hard-coded in the theme’s header.php.
  • There is an output buffering that is used to replace the title.
  • The above method is not appropriate since WordPress provides very easy hooks API for all functionalities.

Due to this Problems that were faced, many experienced community members came forward and suggested to remove this hard-coded title from header.php and instead of this to have some solid filters to filter the automatic title that is generated by WordPress.

Depreciation of wp_title() function with WordPress 4.4 Version: The upcoming release of WordPress 4.4 version that is scheduled to be released on December 2015, the inclusion of wp_title() function in header.php is depreciated. There was also a suggestion that was put forth by the theme authors to add the theme support for ‘title-tag’ rather than hard-coding the title in header.php. The comment that WordPress’s standard theme before the addition of the add theme support code is as follows:

Let WordPress manage the document title.

By adding theme support, we declare that this theme does not use a

hard-coded <title> tag in the document head, and expect WordPress to

provide it for us.

So it was suggested to the theme authors to remove the line to add the title <title> tags with the wp_title() function. Rather in your theme’s function.php, include the below code:

add_theme_support( ‘title-tag’ );

Filtering the title that is being generated by wp_get_document_title():

The theme author himself can also provide the support for the SEO titles, he can filter the title that is being generated by wp_get_document_title by using the ‘pre_get_document_title‘ filter hook as follows:

function filter_the_document_title(){
          return $title;
add_filter( 'pre_get_document_title', 'filter_the_document_title');

NOTE:  In order to get more details about how the value of the title is generated and how it is overridden by the filter hook pre_get_document_title‘ please refer the file wp-includes/general-template.php >> function wp_get_document_title 

One will find all the details regarding the Document title in 4.4 by the article that is submitted by Konstantin Obenland. This code is present in WordPress 4.4 beta and the allied versions.

Leave a Reply

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