Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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');
@thedudster

This comment has been minimized.

Copy link

@thedudster thedudster commented Oct 31, 2018

Can you please give a update on it because i tried it with yoast 9.0.2 and it created the variable but unable to get the data stored under the variable. It shows it same as %%mytitle%%, it was supposed to show 'my name is moss'

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe amboutwe commented Nov 3, 2018

@mrayushh 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.

@devolkpl

This comment has been minimized.

Copy link

@devolkpl devolkpl commented Jul 25, 2019

It's still unsolved bug from Yoast. The value appears in source code but not on the snippet preview.

@skpa2s

This comment has been minimized.

Copy link

@skpa2s skpa2s commented Sep 16, 2019

yo pongo 1 codigo y todo perfecto, pero al ponerlo varias veces, se cae todo wordpress, help!!

@skpa2s

This comment has been minimized.

Copy link

@skpa2s skpa2s commented Sep 16, 2019

I put 1 code and everything perfect, but when you put it several times, all wordpress falls, help !!

/ TITULO SEO 1

// define the custom replacement callback
function get_titulose1() {
return 'dsgshh';
}
// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%titulose1%%', 'get_titulose1', 'advanced', 'titulo seo parte 1' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

// TITULO SEO 2

// define the custom replacement callback
function get_titulose2() {
return 'shgshsh';
}
// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%titulose2%%', 'get_titulose2', 'advanced', 'titulo seo parte 2' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

// META SEO 1

// define the custom replacement callback
function get_metaseo1() {
return 'nfnfnf';
}
// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%metaseo1%%', 'get_metaseo1', 'advanced', 'meta 1 ' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

// META SEO 2

// define the custom replacement callback

@viniciusvts

This comment has been minimized.

Copy link

@viniciusvts viniciusvts commented Jan 8, 2020

thank you, this code help me to resolve an detail in my project

@maheshwaghmare

This comment has been minimized.

Copy link

@maheshwaghmare maheshwaghmare commented May 20, 2020

I have created a quick article with screenshot and example variable current year and current month.

Read more https://maheshwaghmare.com/doc/yoast-seo-custom-template-variable/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.