Created
February 19, 2023 05:24
-
-
Save uddhabh/85f118b16406e9c411c34bb728996f72 to your computer and use it in GitHub Desktop.
Register Custom Variable for Rank Math (state_slug)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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