DEV Community

Sunny Kumar
Sunny Kumar

Posted on • Updated on

Automatically Update Date & Year in WordPress Affiliate Post

We all publish several articles on our website which is related to affiliate products like "Best XYZ Under XYZ Price".

While publishing the post on their blogs, people use the H2 format (as we shared below) to include year or months in their affiliate posts - like I'm using on my lifetime deal website.

image

The main issue with using this type of heading is, When a year or month changed, we have to update them with the latest one and there is no easy way to do it.

Therefore, here is a code which automatically shows the current year;

function year_shortcode() {
  $year = date('Y'); 
  return $year;
}
add_shortcode('year', 'year_shortcode');
Enter fullscreen mode Exit fullscreen mode

And to show the current month along with the year, the code is as follow;

function year_shortcode() {
  $year = date('F Y'); 
  return $year;
}
add_shortcode('year', 'year_shortcode');
Enter fullscreen mode Exit fullscreen mode

It is a basic code which you can include in your function.php file or use plugins like Code Snippets (which I recommend).

Once you've added the code, use the shortcode [year] with your H2 heading or in your paragraphs, like this:

10 Bestselling Mixer Grinder in India [year]

The shortcode will output the current year automatically for you, like this,

10 Bestselling Mixer Grinder in India 2021


This shortcode will not work with SEO title and meta description (Based on your SEO Plugin, there is a separate code for this)

For example, You can use %currentmonth% and %currentyear% in Rankmath and Yoast to show the fresh month and year in SERP Results.

Top comments (0)