Post Reads 237 reads

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.

6 thoughts on “Yoast SEO Custom Template Variable”

  1. How about post year? current year variable is “Post Title 2020”

    What if i want is the post year? the year that i publish the post?

    Example i publish a post in 2017 if i use the current year variable it will be “Post Title 2020”

    But i want it post year variable same as in all in one seo plugin. This was missing in Yoast Seo.

    If i use the post year variable it will be “Post Title 2017”

    The %%date%% variable in Yoast Seo is included the month and day.

    All i want is year. post year, publish year in title.

  2. This is really a interesting question. I have created two snippets which provide the post publish year and post update year snippets as below:

    1) Use the %%post_publish_year%% return the current post publish year. You can use the code from https://gist.github.com/dda3b92b759830931124ae39c2b582f5

    2) Use the %%post_updated_year%% which return the current post updated year. You can use the code from https://gist.github.com/2ae8ea8eabb44b834db05bfb41a248a3

    I’ll update both code snippet and update the article soon.

    1. The code you have given is perfectly working exactly what i want.

      The #1 is not included in yoast variables this feature is what i need, All in one seo already have this kind of feature.

      The #2 in already existing in yoast variable %%currentyear%%.

      Thanks Mahesh for your help!

      Thumbs Up!

  3. Hello Mahesh,

    Do you have article or snippets on how to noindex paginated pages in yoast seo? they removed this option in latest version of yoast seo. they dont give there user an ability to choose if they want to index paginated or not.

    I am currently using snippets to no index paginated pages. that is unrelated to yoast seo.

    The no index tag in paginated pages is above

    I want to used the no index of yoast seo that is properly place where the no index tag is located.

    I thank you in advance,

    Kind regards

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: