Adding Custom Variable to Schema Markup

#496945
Viewing 1 replies (of 1 total)
  • Hello,

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

    Unfortunately, this may not be possible if you are trying to fetch data from javascript. Javascript works on the fly when the page is loaded and our schema generation is basically generated by the backend script which is PHP.

    A solution for this is to create a custom Rank Math variable which you can use in your schema fields. Please refer to this filter guide: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables

    Here’s an example code on how you would return a value for that specific custom variable:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    	   'my_rating_count', [
    	   'name'        => esc_html__( 'Author name', 'rank-math' ),
    	   'description' => esc_html__( 'Author description', 'rank-math' ),
    	   'variable'    => 'my_rating_count', /* the actual variable: %my_rating_count% */
    	   'example'     => my_rating_count_call_back(),
    	   ],
    	   'my_rating_count_call_back'
       );
    });
    function my_rating_count_call_back(){
    	// Retrieve the rating value using PHP
    	return RATING_VALUE;
    }

    So the first step is to determine how to retrieve that rating count value using PHP. You may contact the review plugin you are using and ask them on how you would do it.

    Then, return it inside the “my_rating_count_call_back” function.

    You could then use this variable %my_rating_count% in your schema builder.

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

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘Adding Custom Variable to Schema Markup’ is closed to new replies.