wp_title
Allows changing the page title text generated by the wp_title() function.
This text is usually output in the HTML <title> tag and displayed in the browser tab.
The callback must return a string. If it does not return a value, the title may become empty, and subsequent callbacks may not work correctly.
The filter is applied after the title is assembled and the separator is added, before the result is output or returned.
Use wp_title_parts to change individual parts before they are assembled.
The wp_title() function and this hook are considered conditionally deprecated, but they are still available and are not marked as deprecated.
It is recommended to use title-tag support, which lets WordPress generate the title through wp_get_document_title(), without calling the wp_title filter.
The following hooks are available to configure this title:
- pre_get_document_title - override the title before it is generated. A non-empty value stops further assembly.
- document_title_separator - change the separator.
- document_title_parts - change the parts of the title.
- document_title - change the assembled string.
Usage
add_filter( 'wp_title', 'wp_kama_title_filter', 10, 3 );
/**
* Function for `wp_title` filter-hook.
*
* @param string $title Page title.
* @param string $sep Title separator.
* @param string $seplocation Location of the separator (either 'left' or 'right').
*
* @return string
*/
function wp_kama_title_filter( $title, $sep, $seplocation ){
// filter...
return $title;
}
- $title(string)
- Assembled title, including the separator and spaces around it.
By default, it does not contain the site name. On the site homepage, an empty string is usually passed. - $sep(string)
- Separator passed to wp_title(). Defaults to
»(»). May be an empty string.
By the time the filter is called, the separator is already included in$title. Changing$sepinside the callback will not replace it in the assembled string. - $seplocation(string)
Position of the separator passed to wp_title():
right- to the right of the title. The order of the parts of a compound title is also reversed.leftor another value - to the left of the title.
An empty string
''is passed by default and works asleft.
Examples
#1 Add the site name, description, and page number
Example for a theme that outputs <title> through wp_title().
In header.php, inside <head>:
<title><?php wp_title( '|', true, 'right' ); ?></title>
In functions.php:
add_filter( 'wp_title', 'mytheme_filter_wp_title', 10, 3 );
function mytheme_filter_wp_title( $title, $sep, $seplocation ) {
if ( is_feed() ) {
return $title;
}
$site_name = get_bloginfo( 'name', 'display' );
if ( 'right' === $seplocation ) {
$title .= $site_name;
} else {
$title = $site_name . $title;
}
$description = get_bloginfo( 'description', 'display' );
if ( $description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $description";
}
$page_number = max(
(int) get_query_var( 'paged' ),
(int) get_query_var( 'page' )
);
if ( $page_number >= 2 && ! is_404() ) {
$title .= " $sep " . sprintf(
__( 'Page %s', 'mytheme' ),
$page_number
);
}
return esc_html( $title );
}
The code adds the site name, appends the site description on the homepage and posts page, and adds the page number when paginated. It accounts for both post list pagination (paged) and splitting a single post across pages (page). When called in a feed context, the title remains unchanged.
With the separator on the right, the result for a post will look like this: Post title | Site name.
WordPress attaches esc_html() to this hook with priority 10. A callback in functions.php with the same priority usually runs after it. Therefore, you need to escape any added text yourself, as in the example.
Changelog
| Since 2.0.0 | Introduced. |
Where the hook is called
$title = apply_filters( 'wp_title', $title, $sep, $seplocation );
Where the hook is used in WordPress
add_filter( $filter, 'wptexturize' );
add_filter( $filter, 'convert_chars' );
add_filter( $filter, 'esc_html' );
add_filter( $filter, 'capital_P_dangit', 11 );