adding Custom Variable

#149104
  • Resolved Farid Yazdani
    Rank Math free

    hi, im using yoast right now and have custom variables with this code in function:

    if (defined('WPSEO_VERSION')) {
        wpseo_register_var_replacement('%%en_name%%', 'en_name_replace', 'advanced', 'my title');
    }
    
    function en_name_replace($var, $args) {
      $replacement = null;
      if(isset($args->term_id)) {
        $en_name = get_term_meta($args->term_id, '_name_en', true);
        if(!empty($en_name)) {
          $replacement = $en_name;
        }
      }
      return $replacement;
    }

    i am moving to rank math and i need this variable for title. i found this :

    /**
     * Filter to add custom variables
     */
    add_filter( 'rank_math/vars/replacements', function( $vars ) {
     return $vars;
    });

    and

    /**
     * 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()'
     );
    });

    can someone please help me how to use it?
    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Roel John
    Rank Math business

    Hello,

    Thank you for contacting Rank Math.

    Please see the code below. Let say you want to return create a variable called %post_content% that returns the the_content of the current post. To be able to use the %post_content% you need the code below.

    
    /**
     * 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(
    	   'post_content',
    	   [
    	   'name'        => esc_html__( 'name', 'rank-math' ),
    	   'description' => esc_html__( 'description', 'rank-math' ),
    	   'variable'    => 'post_content',
    	   'example'     => post_content_callback(),
    	   ],
    	   'post_content_callback'
       );
    });
    function post_content_callback(){
    	$content = get_the_content();
    	return $content;
    }
    

    I hope this helps.

    Thanks.

    Farid Yazdani
    Rank Math free

    Thanks.

    Hello,

    Thank you for your update.

    It seems that the filter helped you with your concern. Could you confirm if we can mark this ticket as closed and already resolved?

    Looking forward to your update.

    Thank you

    Farid Yazdani
    Rank Math free

    yes worked very well.

    Anas
    Rank Math business

    Hello,

    We are super happy that your issue is resolved. 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 5 replies - 1 through 5 (of 5 total)

The ticket ‘adding Custom Variable’ is closed to new replies.