How to use Rankmath Variables in post content

#470594
  • Resolved HarsH SHaH
    Rank Math free

    Hello,
    I would like to use Rankmath variables such as %currentmonth% %category% %focuskw% in my post content. Is there any way to do that? Can we create a function and shortcode for this?

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

    Thank you for contacting Rank Math today.

    The only viable we could think that can be used for this would be to create a shortcode that would accept a parameter which would be the variable name and then parse it.

    ​​​​​​You can then parse those using the helper function replace_vars() from our plugin to output the value of the variables.

    Hope this helps point you in the right direction.

    Don’t hesitate to get in touch if you have any other questions.

    HarsH SHaH
    Rank Math free

    First of all, thanks for your reply.

    Can you share a code to create that shortcode?

    Nigel
    Rank Math business

    Hello,

    Below is an example that creates the shortcode [rm_title] for the title. You would need to create similar custom functions for other variables:

    
    add_shortcode('rm_title', 'rm_shortcode_title');
    	function rm_shortcode_title(){
    	$obj = get_queried_object();
    	if(is_single()){
    		$title =get_post_meta($obj->ID, 'rank_math_title', true); 
    		return RankMath\Helper::replace_vars($title);
    	}
    	if(is_tax()){
    		$title = get_term_meta($obj->term_id, 'rank_math_title', true);
    		return RankMath\Helper::replace_vars($title);
    	}
    }
    

    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 3 replies - 1 through 3 (of 3 total)

The ticket ‘How to use Rankmath Variables in post content’ is closed to new replies.