3 ways to rewrite wordpress title

wordpress
スポンサーリンク
language

The normal way to write a title tag in wordpress is as follows.

...
<title><?php wp_title('|', true, 'left'); ?></title>
...

In WordPress 4.4 or later, you can use filters to change the title dynamically.

In this article, we will show you three ways to dynamically change the title using filters.

  • pre_get_document_title filter if you want to change all titles
  • document_title_separator filter If you want to change the title separator
  • pre_get_document_title filter if you want to change part of the title

The title change method I will show you will work with WordPress 4.4 or later.
WordPress does not allow you to change the title from the template.

スポンサーリンク

Print title in wp_head()

First of all, we need to output the title within wp_head().
Add the following code to your functions.php.

<?php

add_theme_support( 'title-tag' );

Previously, we used wp_title() to display the title, but it is now recommended to use add_theme_support() to output the title tag.

Also, since the title tag is output in wp_head(), the following code must be included in header.php.

...
<?php wp_head() ?>
...

Now that you are ready to change the title dynamically, the next step is to show you how to change the title.

Change all of the titles

To change all titles, use the pre_get_document_title filter. <By setting conditional branches in the change_document_title function, it is possible to generate titles for various cases.

<?php

add_filter( 'pre_get_document_title', 'change_document_title' );
function change_document_title( $title )
{
  if( is_archive() )
  {
    // Archive Page
    $title = 'Archives of ' . $title;
  }
  return $title;
}

The output of the title tag is as follows

<title>Archives of Sample</title>

The pre_get_document_title filter is used within wp_get_document_title().
If the pre_get_document_title filter returns a non-empty string, no further processing will be done.

general-template.php in tags/5.7/src/wp-includes – WordPress Trac

Specifically, the title tag will be generated without the following content processing described below.

  • Change the separator.
  • Change part of the title

Changing the separator

The default value for the separator in the title is “-“, so the title tag is output as follows by default.

<title>Sample - Sample site</title>

To change the separator in the title, use the document_title_separator filter.

<?php

add_filter( 'document_title_separator', 'change_document_title_separator' );
function change_document_title_separator( $separator )
{
  return ' |';
}

The output of the title tag is as follows

<title>Sample |Sample site</title>

Used in the document_title_separator filter wp_get_document_title() after the pre_get_document_title filter.

general-template.php in tags/5.7/src/wp-includes – WordPress Trac

If you are using the pre_get_document_title filter, the document_title_separator filter will not work.

Changing a part of the title

To change a part of the title, use the document_title_parts filter.
The parameters that can be used to change the title are as follows

  • Page number for pages with page feed (optional)
  • Tagline for the top page (optional)
  • Site title when not on the top page (optional)
document_title_parts | Hook | WordPress Developer Resources
Filters the parts of the document title.
<?php

add_filter( 'document_title_parts', 'change_title_parts', 10, 1 );
function change_title_parts ( $title )
{
  if( is_archive() )
  {
    // Archive Page
    $title['title'] = 'Archives of ' . $title;
  }
  elseif( is_404() )
  {
    // 404 page
    $title['title'] = 'The page you are looking for was not found.';
  }
  // The following parameters are available
  // $title['page']  = ''; // Page number for pages with page feed (optional)
  // $title['tagline'] = 'catchphrase'; // Tagline for the top page (optional)
  // $title['site'] = 'Site Name'; // Site title when not on the top page (optional)
  return $title;
}

The output of the title tag is as follows

<title>Archives of Sample -Sample site</title>

If you are using the pre_get_document_title filter, the document_title_parts filter will not work.

Summary

In WordPress 4.4 and later, there are three filters to change the title dynamically.

  • pre_get_document_title filter if you want to change all titles
  • document_title_separator filter If you want to change the title separator
  • pre_get_document_title filter if you want to change part of the title

The title can be changed dynamically according to the conditions.
You can use each filter as needed.

タイトルとURLをコピーしました