Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafszul/4505d85a6eb5f7d502382135a75e71ee to your computer and use it in GitHub Desktop.
Save rafszul/4505d85a6eb5f7d502382135a75e71ee to your computer and use it in GitHub Desktop.
Change existing or add custom title or meta template variables
<?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 );
<?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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment