How To Add Dynamically Changing Year In Titles

In this how-to guide, we will teach you how to set up automatically changing years in your content, titles, meta title, and meta description. By following this simple guide

Setting up SEO Framework

To ensure that the year is added correctly to your meta title we highly recommend setting up SEO Framework as your main SEO plugin to your WordPress. It is lightweight, has no bloat, and most importantly works amazingly. This is also required to add the dynamically changing year in meta titles and meta descriptions.

Adding code to functions.php

To add the code to your page you first need to ensure you have set up a child theme and you have a functions.php in your child theme. In the functions.php the first thing we add is a piece of code that allows us to add shortcodes to the title. Normally this is not enabled in WordPress so this is to ensure we can add shortcodes to title fields from now on.

//* Activate shortcode function in Post Title
add_filter( 'the_title', 'do_shortcode' );

Creating the function

The second part we add is the shortcode function itself. This way we can call the shortcode anywhere we want to print the year in that spot. This shortcode works in titles as well as in your articles or pages so you can use it anywhere you want.


//* Shortcode to display the current year in WordPress
//* shortcode: [year]
add_shortcode( 'year' , 'current_year' );
    function current_year() {
    $year = date("Y");
    return "$year";
}

Connecting function to SEO Framework

The third part which is important regarding SEO is to add code so SEO Framework can show the year correctly in meta title and meta description. Without this piece of code, your meta title and meta description will show “[ year ]” instead of the actual current year.

/* Return SEO framework title with year */
add_filter( 'the_seo_framework_title_from_custom_field', function( $title, $args ) {
	return esc_html(get_the_title());
}, 10, 2 );

Conclusion

As you have seen adding the year to the title and meta title and meta description is not hard. With three pieces of code, we get a fully functioning code that allows us to add shortcode to titles, generate the current year dynamically where ever we want it, and are able to add the year to meta title and meta description as well so we don’t have to change it manually.

Here is the full code one more time compiled to one that you can quickly and easily copy-paste to your functions.php file.

//* Activate shortcode function in Post Title
add_filter( 'the_title', 'do_shortcode' );

//* Shortcode to display the current year in WordPress
//* shortcode: [year]
add_shortcode( 'year' , 'current_year' );
    function current_year() {
    $year = date("Y");
    return "$year";
}

/* Return SEO Framework title with year */
add_filter( 'the_seo_framework_title_from_custom_field', function( $title, $args ) {
	return esc_html(get_the_title());
}, 10, 2 );

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *