Last active
March 25, 2025 11:02
Change existing or add custom title or meta template variables
This file contains 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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Remove Yoast SEO Change existing title or meta template variable | |
* Credit: Moshe Harush | |
* https://stackoverflow.com/questions/36281915/yoast-seo-how-to-create-custom-variables | |
* Last Tested: Unknown | |
*/ | |
// define the wpseo_replacements callback | |
function filter_wpseo_replacements( $replacements ) { | |
if( isset( $replacements['%%page%%'] ) ){ | |
$replacements['%%page%%'] = 'Page x of y'; | |
} | |
return $replacements; | |
}; | |
// Add filter | |
add_filter( 'wpseo_replacements', 'filter_wpseo_replacements', 10, 1 ); |
This file contains 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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Remove Yoast SEO Add custom title or meta template variables | |
* Credit: Moshe Harush | |
* https://stackoverflow.com/questions/36281915/yoast-seo-how-to-create-custom-variables | |
* Last Tested: Nov 29 2018 using Yoast SEO 9.2.1 on WordPress 4.9.8 | |
******* | |
* NOTE: The snippet preview in the backend will show the custom variable '%%myname%%'. | |
* However, the source code of your site will show the output of the variable 'My name is Moses'. | |
*/ | |
// define the custom replacement callback | |
function get_myname() { | |
return 'My name is Moses'; | |
} | |
// define the action for register yoast_variable replacments | |
function register_custom_yoast_variables() { | |
wpseo_register_var_replacement( '%%myname%%', 'get_myname', 'advanced', 'some help text' ); | |
} | |
// Add action | |
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables'); |
if someone needs to replace the custom variables also in the backend snippet preview, this is an example
https://gist.github.com/erikyo/53ec6a1b2264135dae95e866194a0a8a
How would I duplicate an existing template variable and modify it?
Or, in other words, how can I access $replacements['%%category_description%%']
in my replacement callback function?
Ah so this is really just a wrapper for the yomama and not a generic solution for any custom field i add. While it is nice it shows the value (although it does say undefined and a random number) it is not really suitable for an app where we wish to add 10 of these custom fields to use?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@amboutwe it is possible to render the custom template variables in the snippet preview? thanks!