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