Shortcode in Rank Math SEO Preview

#81252
  • Resolved Fabio
    Rank Math free

    Hello,
    is it possible to have a shortcode be rendered in Rank Math post preview?

    I have the classic [year] shortcode defined and working as a PHP snippet to dynamically display the current year in the post title.

    add_shortcode('year', function() {
    	return date('Y');
    } );
    add_filter('the_title', 'apply_shortcodes'); // apply to WP title
    add_filter('rank_math/frontend/title', 'apply_shortcodes'); // apply to Rank Math meta title

    It is working as it should on the frontend for both the WordPress title and SEO title (I checked inspecting the page).

    However, it is not displaying correctly in Rank Math post preview. I still see for example “Blablabla [year]”.
    Is there a way to make it work in the post preview too? It would be very helpful for example to correctly count the number of characters and have the actual preview.
    BTW, it works on the WordPress console post list, so I guess there should be a way to make it work on the Rank Math “backend” too.

    Thanks,
    Fabio

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for getting in touch with us.

    You can use the %currentyear% variable in the SEO title or the meta description to show the current year.

    You can also use this code in the functions.php file to process the shortcode in the meta description:

    add_filter( 'rank_math/paper/auto_generated_description/apply_shortcode', '__return_true' );
    

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Fabio
    Rank Math free

    Hi Uzair,
    thanks for your reply.

    I know I could use the %currentyear% variable, but I preferred a solution that wasn’t strictly dependant on any particular SEO plug-in.

    Is there no way to render [year] correctly in the meta title preview?

    Hello,

    I am sorry but the shortcode filter added through your theme functions.php usually renders the data in the front end.

    As a work around you can try and customize the filter to add variable replacements to create a function that gets the shortcode data into the variable which you can then use from the titles & meta dropdown options:

    
    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
     */add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'custom_variable_slug',
     [
     'name'        => esc_html__( 'Custom variable name.', 'rank-math' ),
     'description' => esc_html__( 'Custom variable description.', 'rank-math' ),
     'variable'    => 'custom_variable_slug',
     'example'     => 'custom_variable_callback()',
     ],
     'custom_variable_callback()'
     );
    });
    

    Please note that if you are not a developer, you will need to hire one. As much as we would love, we cant make it to offer custom solutions.

    Hope that helps. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this ticket.