Skip to content

Instantly share code, notes, and snippets.

@uddhabh
Created February 19, 2023 05:24
Show Gist options
  • Save uddhabh/85f118b16406e9c411c34bb728996f72 to your computer and use it in GitHub Desktop.
Save uddhabh/85f118b16406e9c411c34bb728996f72 to your computer and use it in GitHub Desktop.
Register Custom Variable for Rank Math (state_slug)
<?php
add_action( 'rank_math/vars/register_extra_replacements', function() {
rank_math_register_var_replacement(
'state_slug',
[
'name' => esc_html__( 'State Slug', 'rank-math' ),
'description' => esc_html__( 'Displays the slug of the States custom taxonomy in uppercase.', 'rank-math' ),
'variable' => 'state_slug',
'example' => 'VA',
],
'get_state_slug_var'
);
});
function get_state_slug_var() {
global $post;
$terms = get_the_terms($post->ID, 'states');
$state_slug = '';
if ($terms && !is_wp_error($terms)) {
$state_slug = strtoupper($terms[0]->slug);
}
return $state_slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment