Using WordPress ‘document_title_parts’ PHP filter

The document_title_parts WordPress PHP filter allows you to modify the parts of the document title on your website.

Usage

add_filter('document_title_parts', 'your_custom_function');
function your_custom_function($title) {
  // your custom code here
  return $title;
}

Parameters

  • $title (array) – The document title parts:
    • title (string) – Title of the viewed page.
    • page (string, optional) – Page number if paginated.
    • tagline (string, optional) – Site description when on the home page.
    • site (string, optional) – Site title when not on the home page.

More information

See WordPress Developer Resources: document_title_parts

Examples

Add a custom prefix to the title

Add a custom prefix to the title of each page.

add_filter('document_title_parts', 'add_custom_prefix');
function add_custom_prefix($title) {
  $title['title'] = 'Custom Prefix - ' . $title['title'];
  return $title;
}

Remove tagline from the title

Remove the tagline from the title on the home page.

add_filter('document_title_parts', 'remove_tagline');
function remove_tagline($title) {
  unset($title['tagline']);
  return $title;
}

Add the current year to the site title

Add the current year to the site title when not on the home page.

add_filter('document_title_parts', 'add_current_year');
function add_current_year($title) {
  $title['site'] .= ' - ' . date('Y');
  return $title;
}

Reverse the order of title parts

Reverse the order of the title parts.

add_filter('document_title_parts', 'reverse_title_parts');
function reverse_title_parts($title) {
  $title = array_reverse($title);
  return $title;
}

Add custom separator between title parts

Replace the default separator with a custom separator.

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