$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Changing title of a page dynamically from within a plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Changing title of a page dynamically from within a plugin

matteradmin9PV0评论

Every WordPress page can be described as having two titles:

  1. The page/post title, which is displayed within the page/post via the the_title() function call

  2. The html <title></title> tag that displays the title on top of the browser

I am writing a plugin which at one point should change the title of a page dynamically (well, it should change both titles described above).

So, for step 1 above, I found multiple solutions on Stack Overflow (such as this or this). Those are great for only step 1 above.

For step 2, I found this solution; in a nutshell, this is how it works:

add_filter('wp_title', 'change_page_title');
function change_page_title ($title) {

    // Do some magic, and eventually modify $title then return it
    return $title;
}

But the suggested solution is not working for me; and by not working I mean that the filter is not calling the associated function. I am not sure what the problem is; is it because this filter is being called from within the plugin not the theme? (Just FYI, I do not have access to the theme files, so it has to be done from within the plugin).

How can I accomplish this? How can I change the browser title of a page dynamically from within a plugin?

Thanks.

Every WordPress page can be described as having two titles:

  1. The page/post title, which is displayed within the page/post via the the_title() function call

  2. The html <title></title> tag that displays the title on top of the browser

I am writing a plugin which at one point should change the title of a page dynamically (well, it should change both titles described above).

So, for step 1 above, I found multiple solutions on Stack Overflow (such as this or this). Those are great for only step 1 above.

For step 2, I found this solution; in a nutshell, this is how it works:

add_filter('wp_title', 'change_page_title');
function change_page_title ($title) {

    // Do some magic, and eventually modify $title then return it
    return $title;
}

But the suggested solution is not working for me; and by not working I mean that the filter is not calling the associated function. I am not sure what the problem is; is it because this filter is being called from within the plugin not the theme? (Just FYI, I do not have access to the theme files, so it has to be done from within the plugin).

How can I accomplish this? How can I change the browser title of a page dynamically from within a plugin?

Thanks.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Dec 24, 2016 at 5:35 GreesoGreeso 2,2347 gold badges33 silver badges57 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

A post or page has only one title, the title tag <title> is the document title.

The filter wp_title filters the output of wp_title() function, which was used before to output the title of the document. In WordPress 4.1, the title-tag support in themes was introduced and wp_get_document_title() is used instead of wp_title(). So, if your theme supports title-tag, wp_title filter has not effect, but you can use a other filters:

pre_get_document_title to set a new title

add_filter( 'pre_get_document_title', 'cyb_change_page_title' );
function cyb_change_page_title () {

    return "Custom title";

}

document_title_separator to filter the title separator

add_filter('document_title_separator', 'cyb_change_document_title_separator');
function cyb_change_document_title_separator ( $sep ) {

    return "|";

}

documente_title_parts to filter different parts of the title: title, page number, tagline and site name.

add_filter( 'document_title_parts', 'cyb_change_document_title_parts' );
function cyb_change_document_title_parts ( $title_parts ) {

    $title_parts['title'] = 'Custom title';
    $title_parts['page'] = 54;
    $title_parts['tagline'] = "Custom tagline";
    $title_parts['site'] = "My Site"; // When not on home page

    return $title_parts;

}

PD: You can use current_theme_supports( 'title-tag' ) to check if theme supports title-tag or not.

Using this code on you page.php or page which you want to change <pre> <title><?php wp_title(); ?> | <?php bloginfo(‘name’); ?></title> </pre>

Post a comment

comment list (0)

  1. No comments so far