Stay Updated with the YEAR and DATE Shortcodes
This snippet provides dynamic shortcodes for displaying the current year and date on your WordPress site. The shortcodes year and date will always show the correct and up-to-date values. Perfect for sites that need to keep content fresh, like blogs, news outlets, or corporate sites. Additionally, the date shortcode adds a nice touch by displaying the date with the appropriate suffix (e.g., 1st, 2nd, 3rd) for a polished look.
How the snippet works
This snippet utilizes WordPress’s cron job system to keep the shortcodes up to date without overloading the server. The wp_schedule_event function checks if the cron job has been scheduled, and if not, it schedules it to run hourly. This cron job clears outdated transients and ensures that the year and date information is refreshed.
The transients system caches the output of the shortcodes for a set period 7 days; for the year and 1 hour for the date. So your site doesn’t have to query the server every time the shortcode is used. This reduces server load and improves site performance.
The shortcodes themselves work like this:
[year] will output: 2025
[date] will output: 9th July 2025 (the date will always show with the correct suffix based on the day).
[post-published] will output: 15th June 2025 - see it in action below this articleThis setup ensures the shortcodes are both accurate and optimized for performance, offering a sleek and efficient solution for your WordPress site.
// Schedule the cron job if not already scheduled
if (!wp_next_scheduled('delete_date_transient')) {
wp_schedule_event(time(), 'hourly', 'delete_date_transient');
}
// Hook into the scheduled cron job to delete the transient
add_action('delete_date_transient', function () {
delete_transient('shortcode_date_output_' . get_locale());
});
// Shortcode for the current year
add_shortcode('year', function () {
$cache_key = 'shortcode_year_output';
$output = get_transient($cache_key);
if ($output === false) {
$output = esc_html(date_i18n('Y'));
set_transient($cache_key, $output, 7 * DAY_IN_SECONDS); // Cache for 7 days
}
return $output;
});
// Shortcode for the current date with suffix
add_shortcode('date', function () {
$cache_key = 'shortcode_date_output_' . get_locale(); // Locale-aware cache key
$output = get_transient($cache_key);
if ($output === false) {
$day = date_i18n('j');
$suffix = in_array($day, [11, 12, 13])
? 'th'
: ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'][$day % 10];
$output = $day . '<sup>' . esc_html($suffix) . '</sup> ' . date_i18n('F Y');
set_transient($cache_key, $output, HOUR_IN_SECONDS); // Cache for 1 hour
}
return $output;
});
// Shortcode for post published date with suffix
add_shortcode('post-published', function () {
global $post;
if (!is_singular() || empty($post)) {
return '';
}
$timestamp = get_the_time('U', $post);
$day = date_i18n('j', $timestamp);
$suffix = in_array($day, [11, 12, 13])
? 'th'
: ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'][$day % 10];
return esc_html($day . $suffix . ' ' . date_i18n('F Y', $timestamp));
});Written by Milan on 9th July 2025

