Wordpress 4.4+:wp_title过滤器对`< title>`标签没有任何影响 [英] Wordpress 4.4+: wp_title filter takes no effect on the `<title>` tag
问题描述
我只是在我的主题 functions.php
文件中添加以下过滤器:
function change_the_title(){
return'My modified title';
}
add_filter('wp_title','change_the_title');
在我的 header.php中
:
<!DOCTYPE html>
< html<?php language_attributes(); ?>>
< head>
< meta charset =<?php bloginfo('charset');?>>
< meta id =viewportname =viewportcontent =width = device-width>
< link rel =profilehref =http://gmpg.org/xfn/11>
< link rel =pingbackhref =<?php bloginfo('pingback_url');?>>
<?php wp_head(); ?>
< / head>
< body<?php body_class();?>>
然后,我发现我的页面标题 NOT 已更改!标题标记被注入到 wp_head
函数中。
wp_title
在头文件中手动执行,它会返回期望值。 发生什么事?
另外:我的WordPress版本是4.4。
我终于发现wordpress核心代码已经改变,请参阅下面的代码片段。
/ **
*显示带有内容的标题标签。
*
* @ignore
* @since 4.1.0
* @since 4.4.0改进的标题输出替换了`wp_title()`。
* @access private
* /
function _wp_render_title_tag(){
if(!current_theme_supports('title-tag')){
return;
}
echo'< title>'。 wp_get_document_title()。 '< / title>'。 \\\
;
$ b 所以,在4.4之后,核心不会注入 wp_title
结果放入标题< title>
标记中,但对新的函数 wp_get_document_title < code $。
因此,我们可以通过以下方式做同样的事情:
1。直接改变标题:
add_filter('pre_get_document_title','change_the_title');
函数change_the_title(){
return'The expected title';
}
2。过滤标题部分:
add_filter('document_title_parts','filter_title_part');
函数filter_title_part($ title){
返回数组('a','b','c');
}
详情请参阅此处:https://developer.wordpress.org/reference/functions/wp_get_document_title/
PS:寻找函数源代码 wp_get_document_title
是个好主意,其中的钩子告诉了很多人。
I just add the following filter in my theme functions.php
file:
function change_the_title() {
return 'My modified title';
}
add_filter('wp_title', 'change_the_title');
And in my header.php
:
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta id="viewport" name="viewport" content="width=device-width">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<?php wp_head(); ?>
</head>
<body <?php body_class();?>>
Then, I found the title of my page did NOT changed! And the title tag was injected in the wp_head
function.
More, if I call the function wp_title
manually in the header, it do return the expected value.
What's the matter? How can I work around it?
Addition: My WordPress version is 4.4.
解决方案 I finally found out that the wordpress core code was changed, see the below piece of code.
/**
* Displays title tag with content.
*
* @ignore
* @since 4.1.0
* @since 4.4.0 Improved title output replaced `wp_title()`.
* @access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
So, after 4.4, the core do not inject the wp_title
result into the header <title>
tag, but do the same thing with a new function wp_get_document_title
.
So instead, we can do the same thing by:
1. change the title directly:
add_filter('pre_get_document_title', 'change_the_title');
function change_the_title() {
return 'The expected title';
}
2. filtering the title parts:
add_filter('document_title_parts', 'filter_title_part');
function filter_title_part($title) {
return array('a', 'b', 'c');
}
For more, see the details here: https://developer.wordpress.org/reference/functions/wp_get_document_title/
PS: Looking into the source of function wp_get_document_title
is a good idea, the hooks inside which tells a lot.
这篇关于Wordpress 4.4+:wp_title过滤器对`< title>`标签没有任何影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!