Showing the current year in WordPress posts

Learn how to automatically show the current year in WordPress posts by adding a brief code snippet to the functions.php file of your theme.

This article describes how to show the current year in WordPress posts. By automatically generating the current year using a shortcode, you can ensure that your posts are always up-to-date.

Showing the current year

To show the current year in WordPress posts, follow these steps:

  1. Log in to WordPress as the administrator.

  2. On the Dashboard in the left sidebar, click Appearance, and then click Theme Editor:
    WordPress - Dashboard - Appearance - Theme Editor

  3. In the right sidebar, under Theme Files, click Theme Functions (functions.php).

  4. Copy the following code snippet and then paste it at the bottom of the functions.php file:

function current_year() {
    $year = date('Y');
    return $year;
}

add_shortcode('year', 'current_year');
  1. Click Update File. WordPress saves the changes to the functions.php file.

  2. The current year functionality is now enabled, but you must update the post or posts where you want it to appear. To do this, open a post or page on the site, and then type [year] where you want to display the current year. For example:
    WordPress - Current year example - Editing

Save your changes, and then view the post:
WordPress - Current year example - Post

Related Articles