Variable replacement code not working

#134549
  • Here is my code. The log statement says the replacement has been properly registered by the callback is never called. I have the token ‘%smile_feed% in a couple of places.

    function load_rank_math_replacements()
    {
        $test = add_action('rank_math / vars / register_extra_replacements', function () {
            rank_math_register_var_replacement(
                '%smile_test%',
                [
                    'name' => 'smile test',
                    'description' => 'Smile Feed',
                    'variable' => 'smile_test',
                    'example' => 'smile test',
                ],
                'get_feed_parameter'
            );
        });
        error_log('add replacements'.$test);
    }
    
    function get_feed_parameter($var){
        error_log('rankmath:'.json_encode($var));
        return 'test';
    }
    add_action('plugins_loaded','load_rank_math_replacements');
Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello,

    Thank you for contacting Rank Math today.

    Here please use this code instead:

    
    /**
     * 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(
    		'smile_feed',
    		[
    		'name'        => esc_html__( 'Smile name', 'rank-math' ),
    		'description' => esc_html__( 'Smile description', 'rank-math' ),
    		'variable'    => 'smile_feed',
    		'example'     => smile_feed_call_back(),
    		],
    		'smile_feed_call_back'
    	);
    });
    function smile_feed_call_back(){
    	return 'Smile';
    }
    

    We’ve tested this code, and it’s working fine.

    Please see the screenshot below:
    ss-1

    I hope this helps. Let us know if you need further help.

    Thank you.

    ​​​​​​

    Is there a way of getting the token name into the callback? I have a number of these and making a callback for each one is annoying.

    Hello,

    Unfortunately, You can’t put a token inside a custom variable, but you can put a logical condition.

    Please refer to 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(
    		'smile_feed',
    		[
    		'name'        => esc_html__( 'Smile name', 'rank-math' ),
    		'description' => esc_html__( 'Smile description', 'rank-math' ),
    		'variable'    => 'smile_feed',
    		'example'     => smile_feed_call_back(),
    		],
    		'smile_feed_call_back'
    	);
    });
    function smile_feed_call_back(){
        if(is_page( array( 42, 'about-me', 'Contact' ) )){
    	  return 'Smile';
        }
    }
    

    I hope that helps. Let us know if you need further assistance.

    Thank you.

    Not quite what I want. The contents are not based on the page but on the name of the token. for example I have a feed that gets data based on a token. “city” and “state” are two such tokens. I would like the token “facility_city” to return the “city” and “facility_state” to return “state”. Currently I have to do the following code for each token.

    function get_feed_parameter_state() {
    return get_feed_parameter(‘state’);
    }
    It would be better if, instead of a function for each token, I could do something like the following

    function get_feed_parameter() {
    $somevar = gettokenid();
    $token = explode($somevar,”_”);
    $token = $token[1];
    return get_feed_parameter($somevar);
    }
    That function could be registered for all tokens. It there a way of getting the token id from within the vallback

    Hello,

    If you want to get a token id it depends on what function you call and how that function works. If gettokenid() is the one you need to call. You need to call them inside the callback and create a logic inside the call back and return the value based on the logic result.

    Unfortunately, We can’t help you further with the code. We can just provide you the filter on how to return a value in a variable but on creating a logic inside the filter hooks it depends on you on how you want to implement it.

    I hope that helps. Let us know if you have any other questions.

    Thank you.

    Look at the following code:
    rank_math_register_var_replacement(
    ‘smile_feed’,
    $params,
    ‘smile_feed_call_back’
    );
    rank_math_register_var_replacement(
    ‘smile_feed2’,
    $params,
    ‘smile_feed_call_back’
    );

    Both tokens, “smile_feed” and “smile_feed2”, use the callback ‘smile_feed_call_back’. Is there any way the function ‘smile_feed_call_back’ can tell it it was called due to the token “smile_feed” or “smile_feed2”?

    Hello,

    The callback must be unique. You can’t call them multiple times you will encounter an error.

    You can put logic inside a call back itself. That’s how our filter works.

    I hope that answers your question. Let us know if you need more help.

    Thank you.

    First, I never call the callback, your code calls the callback. Registering the same callback twice will not cause an error. It will just cause the same callback to be used for two tokens instead of just one.

    You are still not answering my question. Is there a way of knowing inside the callback what token caused the callback to be done?

    Please talk to someone else about this ticket because you are not understanding what I mean.

    Hello,

    My apology for this one. You’re right you can call multiple callbacks without having an error.

    You can use the code below. To know inside the callback what token caused the callback.

    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    	   'smile_feed',
    	   [
    	   'name'        => esc_html__( 'Smile name', 'rank-math' ),
    	   'description' => esc_html__( 'Smile description', 'rank-math' ),
    	   'variable'    => 'smile_feed(field-name)',
    	   'example'     => smile_feed_call_back(),
    	   ],
    	   'smile_feed_call_back'
       );
    });
    function smile_feed_call_back( $name = '' ){
    	return 'Name ' . $name;
    }
    

    You can call the filter hooks above using this variable %smile_feed(state)%

    Please note this variable will not show any value in the Backend preview. On the frontend, it will work fine.

    The code is similar to the %customfield(field-name)% variable. Please take a look at the URL below.
    https://github.com/rankmath/seo-by-rank-math/blob/master/includes/replace-variables/class-advanced-variables.php#L51-L60

    I hope this helps.

    Thank you, and looking forward to your update.

    That is exactly what I was looking for. Thanks.

    Hello,

    I am glad that this solves/answers your question. Do you need any more assistance with anything else?

    Thank you.

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

You must be logged in to reply to this ticket.