Hello, ladies and gentlemen, in this article I will tell you how to create shortcodes that will allow you to automatically display the next year in the Title and Description of Rank Math, as well as in WordPress article titles (H1), and they will change when the current year changes without your intervention. This shortcode can also be added to the body of the article.

For example, if you want the title to automatically display 2024 in 2025 when writing an article about what needs to be done next year, this feature might be useful to you. At least, you won’t have to change the year manually.

Note that the example with the years I placed above works with the help of shortcodes, which I will teach you to create. Let’s get started.

Automatically Displaying the Next Year in the Title (H1) and Content Body in WordPress

So, we need to implement the output of the year in the “year+1” style. That is, if it is 2024, we need to make it display 2025. Even if you want to implement this only in Rank Math, you still need to follow the instructions here.

To start, you will need a child theme or your own plugin. Of course, you can add the code directly to functions.php, but it will be lost with any update. It’s also not advisable to add it to a child theme. Any code that should work regardless of theme choice should be added to a custom plugin so that changing the theme does not affect this code.

I have explained how to automatically display the current year in H1, title, and description before, and the principle here is the same. We just need to add +1 to the year.

There are two options:

  1. Use a prefix increment that will always add one.
  2. Use the “current year + 1” addition operation.

Both methods are equally good for this task. The prefix increment can cause problems only if the year becomes undefined, for example, due to a server failure. However, in such cases, increment issues will be the least of your concerns. I will not explain this within the scope of the article.

Adding the Code for the First Option

add_shortcode( 'yearone' , 'current_year_one' );
function current_year_one() {
$year = date("Y");
$yearone = (++$year);
return "$yearone";
}
add_filter( 'the_title', 'do_shortcode' );

In this case, we declared a variable responsible for the year output, then added one to this value using a prefix increment and returned the new variable.

If you don’t like this method or it doesn’t work for some reason, you can use this option:

add_shortcode( 'yearone' , 'current_year_one' );
function current_year_one() {
$year = date("Y");
$yearone = ($year + 1);
return "$yearone";
}
add_filter( 'the_title', 'do_shortcode' );

Here, we use the standard “year + 1” addition operation and output the next year.

You can then add this shortcode to H1 or the content:

[yearone]

Both options solve your problem. If you need the format “current year + 5 years,” simply modify the code above.

add_shortcode( 'yearone' , 'current_year_one' );
function current_year_one() {
$year = date("Y");
$yearone = ($year + 5);
return "$yearone";
}
add_filter( 'the_title', 'do_shortcode' );

As you can see, in the variable:

$yearone = ($year + 5);

I just added 5 years instead of one. You can specify any number of years you need.

Bonus: Displaying a Random Year. For those who want to show their whimsical and unpredictable nature, we will output a random year. Add this code:

add_shortcode( 'whatyear' , 'current_year_one' );
function current_year_one() {
$year = date("Y");
$randomyear = rand(1 , 100);
$whatyear = ($year + $randomyear);
return "$whatyear";
}
add_filter( 'the_title', 'do_shortcode' );

With each page reload, a random year from 1 to 100 will be added to the current year. The variable “year” sets the current year as the starting point, and then a random year from 1 to 100 is added.

If you want complete randomness, in the line:

$randomyear = rand( );

Just remove all numbers. Now your year will range from 2024 to 2^64, surprising both visitors and search engines.

Example of displaying a random year in H1

In the articles, H1, Title, or Description, add the following shortcode:

[whatyear]

Don’t forget to add this page to the cache exceptions. Otherwise, everyone will see the last cached result.

Alright, enough joking. Now let’s set up displaying the next year in Rank Math.

Automatically Adding the Next Year in Rank Math Title and Description

To automatically display the year in the format “current year +1” or any other year format in the Rank Math title and description, our shortcodes need to be displayed in the Rank Math plugin. Initially, this does not work.

To make it work, we need to add a new hook to our custom plugin or the functions.php file of the child theme:

add_filter(
	'rank_math/frontend/title',
	function ( $title ) {
		return do_shortcode( $title );}
);
add_filter( 'the_title', 'do_shortcode' );

This code allows the Rank Math plugin to parse shortcodes, enabling the plugin to display them.

Now we can simply add shortcodes to the title in the Rank Math plugin.

Shortcode to display the next year in Title Rank Math

As you can see, there are no problems, and it is practically effortless.

If you need a similar function for the description, then add a similar code for the description:

add_filter(
	'rank_math/frontend/description',
	function ( $description ) {
		return do_shortcode( $description );}
);
add_filter( 'the_title', 'do_shortcode' );

Done! Now you can simply add your previously created shortcodes to the Rank Math description.

Shortcode to output the next year in Description Rank Math

It’s entirely possible to automatically display a year other than the current one.

And it won’t require much effort. You just need to copy the code and add it to your site. By the way, this also works with WooCommerce.

I hope this article helped you, and you managed to implement your idea. With that, I say goodbye, wish you success, and all the best!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

If the materials from this website have been helpful, and you wish to support the blog, you can use the form at the following link: Donate to support the blog