Create title using custom dynamic variables

#384171
  • Resolved Stavros D
    Rank Math free

    Hi,
    is there any way to add dynamic variables to page title?
    I have created a plugin that uses variables to create statistics for countries and champions.
    Right now we use url format like that: /tables/England/Championship
    Is it possible to create page titles that diplay the country and the champion (those two are variables and generated dynamically)?
    e.g. Englands – Championship Table ?
    Where Englands is a variable named var_counrty
    and Championship is another variable named var_championship

    Thanks a lot.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Prabhat
    Rank Math agency

    Hello,

    Thank you for contacting the support.

    You will have to register custom variables to achieve that. This filter is used to register a custom variable: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables

    Below is an example that registers a variable %get_current_date% which returns the current date:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'get_current_date',
            [
                'name' => esc_html__( 'Current Date', 'rank-math' ),
                'description' => esc_html__( 'Gets current date.', 'rank-math' ),
                'variable' => 'get_current_date',
                'example' => 'get_current_date_callback()',
            ],
            'get_current_date_callback'
        );
    });
    function get_current_date_callback(){
        $date = date("j F Y");
        return $date;
    }

    You will need to fetch your variables var_counrty and var_championship in the callback function and return their values.

    Here’s a guide you can follow to add the filter:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Once the variables would be registered, you would be able to use them in the title/description fields.

    Hope that helps.

    Thank you.

    Thanks a lot for the quick reply.
    I will try it and get back asap.

    Hello,

    Sure, let us know how this goes.

    Meanwhile, please don’t hesitate to contact us anytime if you have other questions to assist you further.

    Looking forward to helping you.

    Thank you.

    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 ‘Create title using custom dynamic variables’ is closed to new replies.