Marketing experts such as Neil Patel recommend the addition of the current year to the title tags on your posts, and to some extent, your products. It has the added benefit of letting searchers know that the content is current. In fact, here he is talking about doing just that.
That’s all well and good, but manually updating each title tag is going to be time-consuming. Thankfully, however, you can cut corners and automate the entire process with a few code snippets.
Let’s get started
Step 1. Create a shortcode that displays the year. This shortcode is short and simple and does only one thing. It displays only the year. You may notice that we’ve used the WordPress function, get_the_year(‘Y’). This function can be interchanged with the inbuilt PHP function, date(‘Y’). Being WordPress, we’ll stick with the WordPress way.
/*
* Snippet: Programmatically add the year to your WordPress post and product titles 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?633
* Tested with WooCommerce 8.8.2
*/
add_shortcode( 'year', 'wcsuccess_current_year' );
function wcsuccess_current_year(){
// Return the current year. If you use the WordPress get_the_time('Y') function here it will return the published date of the post
return date( 'Y' );
}
Step 2. Add the year to the actual titles. This requires the use of the add_filter function and needs to target both the title and single post title. If you are using Yoast SEO, the title needs to also use wpseo_title. This code block will turn the shortcode into the current year automatically.
/*
* Snippet: Programmatically add the year to your WordPress post and product titles 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?633
* Tested with WooCommerce 8.8.2
*/
// Add filters to display the date in the title using the function below the filters
add_filter( 'single_post_title', 'wcsuccess_shortcode_title' );
add_filter( 'the_title', 'wcsuccess_shortcode_title' );
// add_filter( 'wpseo_title', 'wcsuccess_shortcode_title' ); // Use this if you are using Yoast SEO. Simply remove the // at the start of this line and add // to the add_filter directly above
// The function to display the shortcode in the title
function wcsuccess_shortcode_title( $title ){
// Here we simply run the WP do_shortcode() function with the $title atribute
return do_shortcode( $title );
}
Step 3. Update the slug so that the shortcode ‘year’ is removed from the slug in the URL. Without the following code snippet a post title of:
Best SEO Snippets of 2024
Will set a slug as follows:
best-seo-snippets-year
That’s not a great looking slug, and once you’ve added dozens, your slugs will begin to look horrible. So how is it handled? By replacing ‘year’ with nothing. But wait. Wouldn’t it be better to use the year instead? Not really. In fact, it could begin to create issues due to 404 errors, duplicate content and the need to update old slugs every year. While a cronjob can be implemented to update the slug yearly, the risk of search engines detecting a new URL as duplicate content and old links landing on 404 pages outweighs any potential benefits.
/*
* Snippet: Programmatically add the year to your WordPress post and product titles 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?633
* Tested with WooCommerce 8.8.2
*/
/**
* Update the slug to remove the year shortcode from the slug
*
* Hook into the transition post status function
*
*/
add_action( 'transition_post_status', 'wcsuccess_slug_update', 10, 3 );
function wcsuccess_slug_update( $new_status, $old_status, $post ){
// Check the value of the new and old status
if ( 'publish' !== $new_status or 'publish' === $old_status )
return;
// Define the slug based on the post title stripping special characters
$new_slug = sanitize_title( $post->post_title );
// Set the year (shortcode name) as the needle to find
$needle = 'year';
// Check if the new slug contains the year
if (strpos($new_slug, $needle) !== false) {
// Here we replace the year shortcode with an empty value. You can modify as needed but read the readme for the reasoning behing an empty value
$new_slug = str_replace($needle, '', $new_slug);
}
// If the current slug does not match the new slug update the post on publish with the new slug
if ( $post->post_name != $new_slug ){
wp_update_post(
array (
'ID' => $post->ID,
'post_name' => $new_slug
)
);
}
}
That’s it. Simply place the code snippets into the functions.php file of your child theme of code snippet plugin and then add the shortcode 2024 to the titles and you’ll be on your way. Because it’s a shortcode, you can also use it in the post body, automatically updating the year at midnight on January 1.
Did this code snippet work for you? Please leave a comment if it did and even add a link to it in action on your website
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch