Using Shortcode on Schema Template

#148147
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention.

    In the screenshot you have provided, it seems that you haven’t entered any shortcode. Could you also give more details about the shortcode you wanted to use in your schema for us to check further?

    Thank you, and we really look forward to helping you.

    Hi,
    I have created shortcodes like [age] for the age, [taxnm] for showing all the taxonomies of CPT with commas and so on.

    Now, I want to use them for the dynamic values in the schema template. Hope it’s clear now. I only figured out that we can use the variables of Rank Math only like %title%, %currentyear%, %url%, and so on.

    But there was no option to use my own shortcode in the schema template.

    Thanks!

    Hello,

    Thank you for the follow up.

    You can consider creating your own custom variables using this filter: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables which will add your variables to be used similar to the existing variables.

    Here is an example implementation:

    
    /**
     * A custom replacement variable to get the current month in custom format
     */
    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'custom_var_name',
     [
     'name'        => esc_html__( 'Custom name', 'rank-math' ),
     'description' => esc_html__( 'Custom name showing custom name', 'rank-math' ),
     'variable'    => 'custom_name_m',
     'example'     => custom_func_callback(),
     ],
     'custom_func_callback'
     );
    });
    
    function custom_func_callback(){
    	
    	return date_i18n( 'this_custom_variable');
    	
    }
    

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

    Hi,
    It would be easier if there was similar code for allowing Shortcode in the schema template.

    Filter to Allow Shortcodes in the Meta Data
    /**
    * Allow shortcodes in the Meta Data.
    */
    add_filter( ‘rank_math/paper/auto_generated_description/apply_shortcode’, ‘__return_true’ );

    Thanks!

    Roel John
    Rank Math business

    Hello,

    Right now, the only way you can use the shortcode in the schema builder is by using a custom variable.

    Please see the screenshot below. I used a custom variable to return a shortcode value. After you add the code to your functions.php in your theme files. You can now use %shortcode_var_name% on your schema or snippet.

    
    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
    * Snippet to register variable that will return all terms in custom taxonomy
     */
    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    	   'shortcode_var_name',
    	   [
    	   'name'        => esc_html__( 'Author name', 'rank-math' ),
    	   'description' => esc_html__( 'Author description', 'rank-math' ),
    	   'variable'    => 'shortcode_var_name',
    	   'example'     => shortcode_var_name_call_back(),
    	   ],
    	   'shortcode_var_name_call_back'
       );
    });
    function shortcode_var_name_call_back(){
        // return your shortcode here
    	return do_shortcode('shortcode');
    }
    

    I hope this helps.

    Thank you.

    Thanks! It worked for me. 🙂

    Hello,

    We are super happy that this custom code was helpful. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Using Shortcode on Schema Template’ is closed to new replies.