In this article we are going to see how to add custom template variable into the Yoast SEO plugin.
We are going to read:
What are the variables in Yoast? What are the variables in Yoast?
Yoast plugin provides the variables to customise the site title. Below are some examples of the variables.
- %%title%%
- %%sitename%%
- %%title%%
- %%primary_category%%
- %%sep%%
- etc
You can see all available variables at:
We can customize the title tag with the help of above variables.
Example of using variables into the title tag Example of using variables into the title tag
Below is the example screenshot of the variables.
Suppose when we use the variables:
%%title%% %%page%% %%sep%% %%sitename%%
Hello world! - Dev
Here,
- Hello world! – Is my post title.
- – (Dash) – Is a Separator
- Dev – Is my site name.
How to add custom variables? How to add custom variables?
WordPress SEO Yoast plugin provide the function wpseo_register_var_replacement
which allow us to add custom variables.
Let’s add current year and current month variable to add into the title variable.
How to add the current Month variable? How to add the current Month variable?
Simply copy and paste below code snippet into your child themes functions.php file.
And use the variable %%current_month%% into the title.
Example Usage
%%title%% %%current_month%% %%sep%% %%sitename%%
The title tag is generated as below:
<meta property="og:title" content="Hello world! May - Dev" />
We can see the current month May into the title variable.
How to add the current Year variable? How to add the current Year variable?
Simply copy and paste below code snippet into your child themes functions.php file.
And use the variable %%current_year%% into the title.
Example Usage
%%title%% %%current_year%% %%sep%% %%sitename%%
The Title tag is generated as below:
<meta property="og:title" content="Hello world! 2020 - Dev" />
We can see the current month 2020 into the title variable.
I see this error while using your code
Your PHP code changes were rolled back due to an error on line 104 of file wp-content/themes/generatepress/functions.php. Please fix and try saving again.
syntax error, unexpected ‘
Oh! Can you please contact me on mwaghmare7@gmail.com and share some additional details about the snippet.
So, I’ll take a look and update the snippet
Just simply remove this code
I see this error while using your code
Your PHP code changes were rolled back due to an error on line 104 of file wp-content/themes/generatepress/functions.php. Please fix and try saving again.
syntax error, unexpected ‘
Oh! Can you please contact me on mwaghmare7@gmail.com and share some additional details about the snippet.
So, I’ll take a look and update the snippet
Just simply remove this code
For adding a different language to the month variable your would need to change the code.
for exemple:
setlocale(LC_ALL, ‘da_DK.utf8’);
return strftime(‘%B’);
For adding a different language to the month variable your would need to change the code.
for exemple:
setlocale(LC_ALL, ‘da_DK.utf8’);
return strftime(‘%B’);