0

How do I edit the tag without using the deprecated wp_title() function? I need to add a microdata parameter - itemprop="name" -, as seen in the example below:

<head itemscope itemtype="http://schema.org/WebSite">
<title itemprop="name">Your WebSite Name</title>
<link rel="canonical" href="https://example.com/" itemprop="url">

The is injected using theme support.

Note 1: All the custom hooks provided by WordPress allow for title content manipulation only, not the tag itself.

Note 2: This is not a duplicate of Custom attribute for the title tag with wp_title() as the accepted answer does not function anymore.

4
  • 1
    You might want to take a look at this answer. It might be the solution you're looking for. Also, Google's Introduction to Structured Data notes that they actually prefer JSON-LD so you might want to look into that too.
    – Cedon
    Commented Jul 24, 2017 at 11:47
  • Thanks, but that solution is not working anymore. I have tried it before asking.
    – Ciprian
    Commented Jul 24, 2017 at 11:59
  • Also, the problem with JSON-LD data is that I am already using 2 SEO plugins which change the title based on various criteria and also generate JSON data. The <title> solution would work best for me.
    – Ciprian
    Commented Jul 24, 2017 at 12:01
  • You might want to contact the authors/maintainers of those plugins and see what function outputs the title then.
    – Cedon
    Commented Jul 24, 2017 at 16:46

1 Answer 1

1

It looks like _wp_render_title_tag() method is what outputs the tag, and the source code in 4.8 is available here: https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/general-template.php#L1083

You can see:

    echo '<title>' . wp_get_document_title() . '</title>' . "\n";

You could first remove the action from wp_head:

    remove_action( 'wp_head', '_wp_render_title_tag', 1 );

Then add your own title render method:

    add_action( 'wp_head', '_wp_render_title_tag_itemprop', 1 );

    function _wp_render_title_tag_itemprop() {
        if ( did_action( 'wp_head' ) || doing_action( 'wp_head' ) ) {     
            echo '<title itemprop="name">' . wp_get_document_title() . '</title>' . "\n";
        }
    }
4
  • This looks good, but will other plugins be affected by this? I mean, can other plugins inject their own code using similar actions?
    – Ciprian
    Commented Jul 24, 2017 at 13:48
  • Other plugins can impact anything, just like your code can! If they wanted to remove the action you added, then yes, that could be removed. From what I've seen in plugins (assuming your concern is SEO plugins [for wp 4.4+]) they usually are using pre_get_document_title to filter the actual title which wp_get_document_title() retrieves, which lies outside of the template render method.
    – Tim Elsass
    Commented Jul 24, 2017 at 14:00
  • Got it! I'll test it and get back to you.
    – Ciprian
    Commented Jul 24, 2017 at 14:24
  • Actually, I just looked at the reference on Note 2 in your question, and it looks like it's pretty much attempting the same way of doing it. I tested in 4.8 and don't have any issues with the code above though.
    – Tim Elsass
    Commented Jul 24, 2017 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.