Yoast SEO Custom Template Variable

Advertisement

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:

https://yoast.com/help/list-available-snippet-variables-yoast-seo/

We can customize the title tag with the help of above variables.

Top ↑

Example of using variables into the title tag Example of using variables into the title tag

Below is the example screenshot of the variables.

Yoast SEO Custom Template Variable 1
Yoast SEO Custom Template Variable 7

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.
Yoast SEO Custom Template Variable 2
Yoast SEO Custom Template Variable 8

Top ↑

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.

Top ↑

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%%
Yoast SEO Custom Template Variable 3
Yoast SEO Custom Template Variable 9

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.

Yoast SEO Custom Template Variable 4
Yoast SEO Custom Template Variable 10

Top ↑

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%%
Yoast SEO Custom Template Variable 5
Yoast SEO Custom Template Variable 11

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.

Yoast SEO Custom Template Variable 6
Yoast SEO Custom Template Variable 12

8 thoughts on “Yoast SEO Custom Template Variable

  1. 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 ‘

  2. 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 ‘

  3. 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’);

  4. 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’);

Leave a Reply