Support » Plugin: Rank Math - SEO Plugin for WordPress » Shortcodes in post title not working

  • Resolved theguitarlesson

    (@theguitarlesson)


    Hi, this looks like a great plugin.

    The only issue I found was that when I use a shortcode in my post title, the plugin doesn’t interpret the output. Specifically, this is the code I use:

    
    // Activate shortcode function in Post Title
    add_filter( 'the_title', 'do_shortcode' );
    
    // Shortcode to display the current year in WordPress
    // shortcode: [year]
    add_shortcode( 'year' , 'current_year' );
        function current_year() {
        $year = date("Y");
        return "$year";
    }
    

    I know that Rank Math has a %currentyear% snippet, but it would be useful if it would treat title shortcodes correctly.

    Thanks,
    Tom

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Rank Math

    (@rankmath)

    Hello @theguitarlesson

    Thank you for contacting the support.

    You need to use a filter to parse the Shortcodes in the title tag. Here it is:
    https://rankmath.com/kb/filters-hooks-api-developer/#change-the-title

    add_filter( 'rank_math/frontend/title', function( $title ) {
        return do_shortcode( $title );
    });

    Hope that helps. If you have any further question(s), please let us know. Thank you.

    Thank you, it worked great. This is a great plugin!

    Plugin Author Rank Math

    (@rankmath)

    Hello,

    Glad it helped.

    Please feel free to contact us again if you need any further assistance.

    We are here to help. Thank you.

    Hii there,
    Can you Please explain where i need to use the above mentioned code for the Title shortcode to work?

    Plugin Author Rank Math

    (@rankmath)

    Hello @nis111

    Please add the above code in your theme’s functions.php file.

    Also, please open a separate topic so the original poster does not get unnecessary email notifications.

    Hope that helps. Thank you.

    theguitarlesson

    (@theguitarlesson)

    Hi, I need to reopen this issue as I found a bug.

    The filter you quoted does parse Title shortcodes and replaces the Title that Rankmath outputs.

    But there are a lot of places in the finished page source code, where the shortcode from the Title was not replaced.

    Here is an example:

    https://pumpupboats.com/remote-control-trolling-motors/

    You can see that the title was parsed correctly, but if you look at the yource code and search for [year], you can see that RankMath did not replace it in numerous other places.

    Thanks,
    Tom

    theguitarlesson

    (@theguitarlesson)

    Thanks,
    Tom

    Plugin Author Rank Math

    (@rankmath)

    Hello @theguitarlesson

    The filter code which we provided was to change the main page’s title. It doesn’t change the content which appears on the page. For eg: https://i.rankmath.com/AG1vWu

    This code is not managed by the Rank Math plugin.

    Hope that helps. Thank you.

    Thanks for the modified code to enable a [year] shortcode to output the current year in <title> tags. Unfortunately using [year] messes up the “Your SEO title doesn’t contain a number.” test and the “URL is ?? characters long.” test.
    My workaround was to rename the [year] shortcode to [y2] which RankMath can see as 4 characters and a number.
    Could RankMath be adjusted to take account of the [year] shortcode or support a set of RankMatch shortcodes, e.g. [rm_year] so the calculations still work?

    I want to use the shortcode in <title>, how to use it? moddroid

    Plugin Author Rank Math

    (@rankmath)

    Hello @bendul

    You can use the following filter to allow Shortcodes in the Meta values:
    https://wordpress.org/support/topic/shortcodes-in-post-title-not-working/#post-11469239

    Hello @renpub

    Sorry for the super-delayed response. We missed your message completely. That’s why WordPress encourages to open a separate support topic always.

    Sorry to disappoint but what you are asking is not possible as the filter code works only on the frontend. In the backend, all the data is processed via JS file which doesn’t have any method to decode a Shortcode.

    If there’s anything else we can help you with, please let us know.

    Thank you.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Shortcodes in post title not working’ is closed to new replies.