I add this code to function.php but now as I understand it, I need a function to make it all work
Can you help me with this
/**
* 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(
‘gorod’,
[
‘name’ => esc_html__( ‘gorod.’, ‘rank-math’ ),
‘description’ => esc_html__( ‘gorod.’, ‘rank-math’ ),
‘variable’ => ‘gorod’,
‘example’ => ‘gorod_callback()’,
],
‘gorod_callback()’
);
});
//your callback function
function gorod_callback(){
return ‘gorod’;
}
I make it in Yoast with this code and it work
How can I make it work in Rank Math
function get_city() {
$result = '';
$obj = get_queried_object();
if ( ($obj instanceof WP_Post) && ($obj->post_type == 'rtcl_listing') ){
$post_id = $obj->ID;
$obj = get_the_terms($post_id, 'rtcl_location');
$city_id = array_pop($obj);
$city_id = $city_id->term_id;
$city_phrase = get_term_meta($city_id, 'gorod', true);
if (strlen($city_phrase) > 0){
$result = $city_phrase;
}
}
return $result;
}
// define the action for register yoast_variable replacments
function incity_variables() {
wpseo_register_var_replacement( '%%incity%%', 'get_city', 'advanced', 'Склонение' );
}
add_action('wpseo_register_extra_replacements', 'incity_variables');
Hello,
Thank you for contacting Rank Math today.
To use the above function you need to modify your callback function to get the value of the custom field. The following is an example but you will need the help of a developer, or the ACF support to correctly customize this:
function gorod_callback(){
$gorod = get_field( "your_ACF_name" ); //get field value from current page
return $gorod;
}
Hope that helps. If you have any further question(s), please let us know. Thank you.
I tried to rewrite the code but it does not work
what am I doing wrong
/**
* 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(
'incity',
[
'name' => esc_html__( 'incity.', 'rank-math' ),
'description' => esc_html__( 'incity.', 'rank-math' ),
'variable' => 'incity',
'example' => 'incity_callback()',
],
'incity_callback()'
);
});
//your callback function
function incity_callback() {
$incity = '';
$obj = get_queried_object(); // Get the current post
if ( ($obj instanceof WP_Post) && ($obj->post_type == 'rtcl_listing') ){
$post_id = $obj->ID; // fix the post ID
$obj = get_the_terms($post_id, 'rtcl_location');
$city_id = array_pop($obj); // We returned a taxonomy tree with a full hierarchy - but we only need the last element, and we take it
$city_id = $city_id->term_id; // fix the id of the final taxonomy
$city_phrase = get_term_meta($city_id, 'gorod', true); // fetching the field value
if (strlen($city_phrase) > 0){
$incity = $city_phrase; // We replace the default value with the received one
}
}
return $incity;
}
Hello,
As much as we wanted, we can’t solve issues in code customizations since our team is small. I would recommend you to hire a dev that could fix that code for you.
Anyway, if you have any particular questions in the code, we will be happy to help.
Looking forward to help 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.