Registering Custom variables

#387461
  • Resolved Guga Alves
    Rank Math free

    Hi there,

    I work on The Events Calendar support team, and based on a customer request we’re trying to get some data from our plugin and let Rank Math use it as Extra Variables on the Meta box Snippet Editor. I’m testing it first adding the rank-math.php file on twentytwenty theme, but once it works our developers will create an extension.

    Following your documentation at https://rankmath.com/kb/make-theme-rank-math-compatible/#add-extra-variables, I was able to register the new variables and using var_dump I can see that the callbacks are correctly getting the data, but they’re not shown when I test adding the variable on the Meta Box Snippet Editor.

    Here’s the code I’ve tried so far:


    <?php
    add_action('rank_math/vars/register_extra_replacements', 'tec_rm_register_fields', 10,2);

    function tec_rm_register_fields(){

    rank_math_register_var_replacement(
    'event_start_date',
    [
    'name' => esc_html__('Event Start Date','rank_math'),
    'description' => esc_html__('Get this Event start date','rank_math'),
    'variable' => 'tec_event_start_date',
    'example' => tec_seo_variable_get_event_start_date(),
    ],
    ['tec_seo_variable_get_event_start_date']
    );
    rank_math_register_var_replacement(
    'event_start_end',
    [
    'name' => esc_html__('Event End Date','rank_math'),
    'description' => esc_html__('Get this Event end date','rank_math'),
    'variable' => 'tec_event_end_date',
    'example' => tec_seo_variable_get_event_end_date(),
    ],
    ['tec_seo_variable_get_event_end_date']
    );
    }

    // Functions retrieving data for the variables
    // Reference: Date and time functions available at /the-events-calendar/common/src/functions/template-tags/date.php

    function tec_seo_variable_get_event_start_date(){
    if (isset($post)){
    $date_format = tribe_get_date_format();
    $formatted_start_date = tribe_get_start_date( $post->ID, false, $date_format );
    var_dump($formatted_start_date);
    return $formatted_start_date->string;
    }
    }

    function tec_seo_variable_get_event_end_date(){
    if (isset($post)){
    $date_format = tribe_get_date_format();
    $formatted_end_date = tribe_get_end_date( $post->ID, false, $date_format );
    return $formatted_end_date->string;
    }
    }

    Another difficulty that I’m having is making those functions work only on the post editor screen, so that’s why I’ve used isset($post), but I don’t know if that’s the best approach.

    Thank you in advance.

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

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    Did you test the source code of the page where those variables are being added to make sure that the results are correct?

    Please note that custom-created variables won’t be parsed in the SERP preview of our plugin but if they are getting the correct data they should get parsed when the page loads.

    ​​​​​​​Looking forward to hearing back from you.

    Hi there,

    > Did you test the source code of the page where those variables are being added to make sure that the results are correct?

    Yes, I’ve tested it, using var_dump at each line I can see that all the variables on the Start Date and End Date functions are returning properly, but the return $variable is not passing the argument to the Meta box Snippet Editor.

    Also, we have a similar snippet for Yoast SEO and it works properly there, so our functions are really correct.

    Hello,

    The value of the variable will not be parsed in the snippet preview of the meta box. However, it will be applied correctly in the frontend.

    Please check the meta tags in the page source to confirm if the variable is returning the correct values or not.

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

    Thanks.

    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 ‘Registering Custom variables’ is closed to new replies.