document_titlefilter-hookWP 5.8.0

Allows you to change the page's <title>.

This hook changes the fully generated title — the final string. In contrast, the similar document_title_parts hook allows individual title parts to be changed.

The hook works only if:

  • Theme support for 'title-tag' is enabled.
  • The add_action( 'wp_head', '_wp_render_title_tag', 1 ); hook has not been disabled.

Usage

add_filter( 'document_title', 'wp_kama_document_title_filter' );

/**
 * Function for `document_title` filter-hook.
 * 
 * @param string $title Document title.
 *
 * @return string
 */
function wp_kama_document_title_filter( $title ){

	// filter...
	return $title;
}
$title(string)
The page title.

Examples

#1 Change the front page title

For the site's front page, WordPress generates site title + description in the title by default, as specified on the “Settings -> General” page. For example:

My Test Site — Just another WordPress site

Change the title of the front page only:

add_filter( 'document_title', 'modify_document_title_for_front_page' );

/**
 * Changes the title of the site's front page.
 *
 * @param string $title
 *
 * @return string
 */
function modify_document_title_for_front_page( $title ) {
	return is_front_page() ? 'This title is only for the front page' : $title;
}

Changelog

Since 5.8.0 Introduced.

Where the hook is called

wp_get_document_title()
document_title
wp-includes/general-template.php 1503
$title = apply_filters( 'document_title', $title );

Where the hook is used in WordPress

wp-includes/default-filters.php 164
add_filter( $filter, 'wptexturize' );
wp-includes/default-filters.php 165
add_filter( $filter, 'convert_chars' );
wp-includes/default-filters.php 166
add_filter( $filter, 'esc_html' );
wp-includes/default-filters.php 171
add_filter( $filter, 'capital_P_dangit', 11 );