document_title_parts
Allows you to change the title of a WordPress site page.
This is a filter for wp_get_document_title(), which builds the page title. When add_theme_support( 'title-tag' ) support is enabled, WordPress outputs it in the HTML HEAD on the front end.
This filter lets you change the individual parts from which the title is built.
Use the document_title hook to change the fully assembled title.
Usage
add_filter( 'document_title_parts', 'wp_kama_document_title_parts_filter' );
/**
* Function for `document_title_parts` filter-hook.
*
* @param array $title The document title parts.
*
* @return array
*/
function wp_kama_document_title_parts_filter( $title ){
// filter...
return $title;
}
- $title(array)
Associative array containing every title part. The parts are then joined with the
-separator. The separator can be changed through the document_title_separator filter.Array elements:
-
title(string)
Title of the page being viewed. -
page(string)
Text for a paginated page. Generated from one of the global variables:global $page, $paged. -
tagline(string)
Site tagline on the home page, is_front_page(). - site(string)
Site name on every page except the home page.
-
Examples
#1 Change the title of the portfolio page
add_filter( 'document_title_parts', 'filter_function_name_2114' );
function filter_function_name_2114( $title ){
if( is_page('portfolio') )
$title['title'] = 'My portfolio page — Dexter Morgan';
return $title;
}
#2 Remove the site name from the end of the title
The site name is appended to titles on all singular pages with a separator. This is not always desirable, so it can be removed:
add_filter( 'document_title_parts', function( $parts ){
if( isset($parts['site']) )
unset($parts['site']);
return $parts;
});
#3 Remove the site tagline from the home page title
By default, the home page title uses the "Site name - site tagline" pattern. Use this code to remove the tagline:
// Remove the site tagline from the home page title.
add_filter( 'document_title_parts', function( $title ){
if( isset($title['tagline']) )
unset( $title['tagline'] );
return $title;
});Changelog
| Since 4.4.0 | Introduced. |
Where the hook is called
$title = apply_filters( 'document_title_parts', $title );