Enable Shortcode in Page Titles on pages and menus but not browser tab


I am using the following code to enable shortcode in Page Titles.add_filter( 'the_title', 'do_shortcode' ); I don't have to use it often but when I do it works well for swapping content in both the titles on the page and the menus. The problem is that the shortcode displays in the title on my browser tab-see screenshot attached. Is there a way to modify the filter to also enable the shortcode in browser tabs, or, create another filter to enable shortcode in the tabs, or if not, possibly manually modify the few individual pages that need the shortcode in the title? I'm having trouble finding a solution online and was hoping someone might have some ideas to tear me in the right direction. Thank you!

  • eschmidtke

    Hi Adam,

    I looked into it a bit further and realized I didn't really have an understanding of the difference between wp_title and the_title. Now your answer makes more sense to me. I added the snippet to my functions.php and it did not affect the title in the tab (now I agree that should work) but if all else fails and we can't figure out why that is not working in my case, Im wondering if I could use a conditional within the title tag in head to selectively change the titles for specific pages. something like... if(is_page('id") echo "the desired title" else (wp_title); I can figure out the code, Im just wondering if that sounds logical or is there a better way? Ideally I would like to go your route cuz my hack idea would be a giant pain. Thanks again for your thoughts here.

  • Adam Czajczyk

    Hello eshmidtke!

    I admit I expected this as it's suggested to not use "wp_title" anymore in themes, instead leaving handling HTML "title" tag to WP core. Fortunately, the "wp_title()" function (template tag) is not remove and you could try a little workaround.

    I'm thinking of following solution:

    1. create the basic child-theme of your current theme; here's a handy guide on this:

    2. The only file we'd like to address with child theme would be the "header.php" file

    3. In this file you would like to (between "head" opening and closing HTML tags and before "wp_head()") the "wp_title()" like this:

    /... current code goes here .../
    <title><?php wp_title(' - ", true, ' '); ?></title>
    <?php wp_head(); ?>

    4. Move the code I previously suggested from parent theme's "functions.php" file to child theme's function.php file

    5. Switch WP to use child-theme

    This should cause your theme to use "wp_title()" and as a result the filter I shared previously should start working. I just tested this on my test setup with a success.

    Best regards,

  • Adam Czajczyk

    Hello eschmidtke,

    Thanks for granting access!

    I checked your site and it seems that it indeed doesn't play well with "wp_title()". That said, since it already deprecated function and is going to be removed from WordPress anyway I found another solution.

    1. I removed the filter for wp_title() first
    2. I made sure that the "wp_title()" function is not used in your parent and child theme's header
    3. I moved this code from you parent to child theme's functions.php file:

    add_filter( 'the_title', 'do_shortcode' );
        //you might be able to simplify this to just "add_filter( 'the_title', 'do_shortcode' )", though I am not 100% sure of that...
        add_filter('widget_text', 'do_shortcode');

    4. I added this code to it:

    add_filter('document_title_parts', 'wpmu_doc_title_shortcode', 10);
    function wpmu_doc_title_shortcode($title) {
    	$my_title = $title['title'];
    	$my_title = do_shortcode($my_title);
    	$title['title'] = $my_title;
        	return $title;

    It seems to be working now. The "document_title_parts" hook is not well documented yet and was introduced with WP 4.x I think (maybe even with 4.4.x). A word of explanation why I"m "rewriting" title "back and forth" to and from "$my_title" variable: that's because using an array as a "do_shortcode()" parameter causes function to break.

    That said, please check if it's working now!

    Best regards,

  • eschmidtke

    Hi Adam. Yes! it works now. Thank you so much, I would have never figured that out on my own. So I tried it on yet another version of this theme without creating a child theme and it appears to work (after deleting wp_title in head). So with this revised solution you provided, is it necessary to create a child theme or was that suggestion only to protect the original parent theme? I have been using underscores as a theme base and it is not recommended to use child themes.

    Thanks again for your persistence with my issue. It is truly appreciated and I learned a lot from your explanations.

  • Adam Czajczyk

    Hello eschmidtke!

    I'm really glad that I helped!

    So with this revised solution you provided, is it necessary to create a child theme or was that suggestion only to protect the original parent theme?

    It's not necessary to use a child theme. I suggested it because it's a recommended and safe way to change theme at file/code level. Also, all changes applied to parent/original theme directly would be lost upon first theme update.

    There's however another solution that'd be safe, updated-independent and easy to apply. Here's what you may do:

    - cut all that additional code from "functions.php" file
    - create and empty file with a .php extension (e.g. "shortcodes-in-titles.php")
    - paste that code to that new file
    - make sure that the very first line (above the code) is a php opening tag:


    and the code is below it
    - save file
    - access your server via FTP
    - go to "/wp-content" folder
    - if there's no "mu-plugins" folder inside "/wp-content" folder, create it
    - upload your file to"/wp-content/mu-plugins" folder

    What have you just implemented is called "Must Use plugin" (MU plugin). It's a special kind of WordPress plugin that's automatically loaded and activated and doesn't require any interaction from you (it "just works"). This should help, I think.

    Best regards,

  • Adam Czajczyk

    Hey eschmidtke!

    I have one more question. I noticed on the pages that are implementing shortcode in the titles have the entire url (http://..) in the tab.

    I'm not sure if I follow you on this. I visited your site again but either I'm not seeing the same as you are or I'm just missing something here. Could you please point me to example pages where I could check it? An accompanying screenshot would be also helpful :slight_smile:

    Best regards,

  • eschmidtke

    Sorry, I was incorrect, I had inconsistencies due to caching. Everything seems ok now however this is the odd thing... My old pages using wp_title display the names of the pages only in the browser tabs (firefox and chrome) and the version of my site incorporating the filter using document_title_parts displays the entire url in the tab. I am attaching 2 screen shots. One is an older version of a staging site using wp_title and the other is our new code.

    Not sure if this can be adjusted or not I just thought it would be interesting to you if you didn't already know I would get this result.

  • Adam Czajczyk

    Hello eschmidtke,

    I can see now. I was visiting the page that's associated with this thread and it seems that it's working fine there. Is this the same theme with the same settings? The "title parts" screenshot refers to the site under different domain. I understand that's a "live" site but is there any chance that I could have a look (I will not make any changes) there from the "inside"?

    Best regards,

  • eschmidtke

    Hi Adam,
    Sorry for the delay. I installed your WPMU plugin and granted access to my live site. This is an older theme which contains a lot of sprawl which is probably my problem. The one we have been working on is going to be a new version of the theme (same design in the end) with intentions of improving functionality and code. You have been fantastic and I totally appreciate your ongoing attention to my issue. Thanks again.

  • Adam Czajczyk

    Hello eschmidtke,

    Thanks for granting access!

    I reviewed the site and I think it would be worth to give it a bit more "love and caring" :slight_smile:

    So far I found out that there are two "header" files that are used on the site: some templates call "standard" header and some call "minimal". One of these headers do use "wp_title()" and the other don't. Furthermore, in your functions.php file there are both filters enabled:

    "add_filter('wp_title', 'do_shortcode');"

    and the "document_title_parts" one.

    I think this is in general what may be causing an issue here since we already know that "document_title_parts" should work and is working on your other site.

    I'd try this:

    - make all headers consistent, meaning that I'd get rid of

    <title><?php wp_title();?></title>

    - comment out the

    add_filter('wp_title', 'do_shortcode');

    in functions.php

    This should cause the site to use default WP functions to output titles that would then be affected only by the "document_parts_title" filter that seems to be working elsewhere. In case it didn't help, those changes are fortunately easy to revert :slight_smile:

    I didn't make these changes myself because I realize it's a live site and a business so I wouldn't want to risk breaking it in case anything went wrong (and I wouldn't be able to undo changes then via support access).

    Best regards,

  • eschmidtke

    Ha yeah, thats why Im starting over. I had no idea what I was doing not to mention way too complicated of a site for a beginner. BUT I learned a lot tackling this project.

    I did a search in my functions.php folder and I don't see that I am using add_filter('wp_title', 'do_shortcode'); anywhere. I only see only add_filter('the_title', 'do_shortcode'); I deleted <title><?php wp_title( '|', true, 'right' ); ?></title> in the header-minimal.php file.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.