24. Add Extra Variables to the Rank Math Dropdown – error

#493702
  • Resolved Josef Kuchař
    Rank Math free

    Hello,
    I´m trying to add custom values to titles by using this https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables . So I added this code:

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

    });

    function custom_variable_callback()
    {
    return "test";
    }

    to the functions.php in WordPress, “Custom variable name.” is shown in the dropdown menu so I add it and then it displays error on the page where it should display the value. The error is:
    Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, function "custom_variable_callback()" not found or invalid function name . (the whole error is in sensitive data box). What is wrong with that? Where do I make a mistake? pls help.

    Thank you a lot.

    If there would be a way to use classic WordPress function it would be great as well. I have custom plugins that uses wp shortcodes, but it does not work with Rank Math.

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

    Thanks for contacting us, and sorry for any inconvenience that might have been caused due to that.

    Can you please try replacing the filter you’ve shared above with the following one to see if that works for you?

    /**
     * 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'
    	);
    });
    
    function custom_variable_callback() {
    	$yourVariable = "test"; //Assign your value here
            return $yourVariable;
    }

    You should be able to find the newly created variable %custom_variable_slug% using above filter.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Hello,
    thank you it´s working now. Is the only difference that you assign value to the variable and then return it? I never thought there might be a difference between that and returning it straight away.

    Kind Regards,
    Josef

    Hello,

    No, the error is coming from the line after the end of the array where you are calling the function before it’s defined.

    If you check the code snippet we shared you’ll see that is missing the parenthesis which makes it work.

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

    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 ‘24. Add Extra Variables to the Rank Math Dropdown – error’ is closed to new replies.