Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 217 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wp_title filter takes no effect on the <title> tag

#1
I just added 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** change! And the title tag was injected in the `wp_head` function.

More, if I call the function `wp_title` manually in the header, it does return the expected value.

What's the matter? How can I work around it?

---

Addition: My WordPress version is 4.4.

Reply

#2
Not sure if its necessary to inject the variable, but try this.

function change_the_title($title) {
return 'My modified title';
}
add_filter('wp_title', 'change_the_title');
Reply

#3
you are missing `title` in your `head` tag, Add In `<head>` tag

`<title><?php wp_title('|', true, 'left'); ?></title>` your wp_filter will work normally.
Reply

#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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through