wp_title
云策文档标注
概述
wp_title 是一个 WordPress 过滤器,用于修改页面标题文本,影响 HTML
关键要点
- wp_title 过滤器用于过滤页面标题,作用于 wp_title() 函数生成的 HTML
标签,而非文章、页面或分类标题本身。 - 过滤器参数包括 $title(标题字符串)、$sep(分隔符)和 $seplocation(分隔符位置,可选 'left' 或 'right')。
- 注册过滤器时需使用 add_filter('wp_title', 'filter_function_name', 10, 3),其中 filter_function_name 必须是唯一的函数名,且函数必须返回处理后的标题字符串。
- wp_title 在历史上曾被标记为弃用,但后来已恢复使用;建议开发者关注相关替代过滤器如 pre_get_document_title、document_title_separator 和 document_title_parts。
代码示例
add_filter( 'wp_title', 'filter_function_name', 10, 3 );
function filter_function_name( $title, $sep, $seplocation ) {
// 自定义标题处理逻辑
return $title;
}注意事项
- 过滤器函数必须返回标题字符串,否则可能导致标题空白或引发其他插件错误。
- 函数名 filter_function_name 不能与已声明的其他函数名冲突,需确保唯一性。
- 虽然 wp_title 当前可用,但建议开发者了解并考虑使用 WordPress 4.4 引入的文档标题相关过滤器以保持代码前瞻性。
原文内容
Filters the text of the page title.
Parameters
$titlestring-
Page title.
$sepstring-
Title separator.
$seplocationstring-
Location of the separator (either
'left'or'right').
Source
$title = apply_filters( 'wp_title', $title, $sep, $seplocation );
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 5 content
Andrew Klimek
wp_title is being deprecated, see https://make.wordpress.org/core/2015/10/20/document-title-in-4-4/
Please use:
pre_get_document_title– short-circuitswp_get_document_title()if it returns anything other than an empty value.document_title_separator– filters the separator between title parts.document_title_parts– filters the parts that make up the document title, passed in an associative array.Skip to note 6 content
acuvic
Just for information, wp_title has been reinstated:
“UPDATE 12 November – wp_title has been reinstated until alternative usages have been identified and a path forward for them defined.”
Reference link as originally given by Andrew Klimek above https://make.wordpress.org/core/2015/10/20/document-title-in-4-4/
Skip to note 7 content
Drew Jaynes
Basic Example
In header.php:
<title></title>In functions.php:
/** * Creates a nicely formatted and more specific title element text * for output in head of document, based on current view. * * @param string $title Default title text for current view. * @param string $sep Optional separator. * @return string Filtered title. */ function wpdocs_filter_wp_title( $title, $sep ) { global $paged, $page; if ( is_feed() ) return $title; // Add the site name. $title .= get_bloginfo( 'name' ); // Add the site description for the home/front page. $site_description = get_bloginfo( 'description', 'display' ); if ( $site_description && ( is_home() || is_front_page() ) ) $title = "$title $sep $site_description"; // Add a page number if necessary. if ( $paged >= 2 || $page >= 2 ) $title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) ); return $title; } add_filter( 'wp_title', 'wpdocs_filter_wp_title', 10, 2 );Skip to note 8 content
Rafa Carvalhido
Example updated with the third parameter of the filter:
add_filter( 'wp_title', 'filter_function_name', 10, 3 ); function filter_function_name( $title, $sep, $seplocation ) { // the magic return $title; }