How to add Date Last Updated to WordPress Posts

Most themes show the Published Date above each post. What most themes don’t show (for some reason) is the Last Updated (or Last Modified) Date/Time. This sucks, because no one (including Google) will be able to fully appreciate the work you put into your blog!

Luckily WordPress already saves this information for each post and it is pretty easy to integrate into your theme using a plugin. Today I’ll show you how.

Is it important to show the Last Updated Date/Time?

Yes.

Google rewards well performing websites, i.e. websites that are following their rules.

One of their rules is: regular updates — within reason. On the other hand, a website with stale content will drop in the search results over time.

Bots crawl a page for a schema.org property called datePublished and derive the published date from it. This gives bots an idea of the freshness of its content and the entire website.

But what if you recently made some additions to a post you’ve written a few years ago? Without a clear indication of when your post was last modified, the content will still seem stale regardless of your efforts!

Enter dateModified: a schema.org property used for bots to identify the date on which your post was most recently modified.

Awesome! How do I add this?

Well, there’s the easy way, and there’s the right way.

I wouldn’t be me, if I didn’t suggest alternatives to my — slightly more amazing — solution.

One (very popular) plugin in the WordPress repository is WP Last Modified Info, an easy to use, easily customizable plugin. Simply put, it does the job, but feels a bit bloated.

Another (more lightweight than the above plugin, and possibly easier to implement than this tutorial) alternative is following WP Beginner’s tutorial.

I sense a ‘but’ coming…

But.

Both solutions fall short, in my opinion. They make the same ‘mistake’ of adding the Date Last Updated directly before the content. Which serves its purpose for SEO optimization, but feels unnatural and looks a bit clumsy.

WP Beginner's implementation of Last Modified Date.
WP Beginner and the Plugin both add the date Last Updated directly before/above the content.

A more elegant and user-friendly solution would be to add the last modified date next to (or at least near) the published date.

Well-crafted themes add the Published Date to the template by using the_date() or an equivalent, such as get_the_date() or the_time(). All these functions offer a filter with the same name, which we can hook into.

  • The Wait is Over.

    Get the Newsletter you've always wanted, now!

    Sign up to receive Biweekly, Free Optimization Tips for WordPress.

    No spam. I promise.

  • How do I know which Filter to use?

    Example of the Published Date element on Daan.dev
    Take note of the value (entry-date) of the class attribute in the Highlighted part.

    It’s relatively easy to find out which filter you’d have to hook into. Just visit one of your blog posts (in the frontend) and:

    1. Right-click on its published date and click ‘Inspect’. Some browsers call it ‘Inspect Element’.
    2. A window, called DevTools, pops up with information about the entire website and more importantly, the Published Date element.
    3. Within the highlighted lines, take note of the value of the class attribute.
    4. Search your theme’s files for this value and there should be a function-call on the same line, e.g. get_the_time('Y-m-d') or the_date().

    9 out of 10 times the name of the function equals the name of the filter you should use. If it doesn’t work, leave a comment with the theme you’re using and I’ll try to help you out.

    How to Insert the Last Updated Date next to Date Published

    In this example we’ll be using WordPress’ Twenty Twenty theme, which uses the_date() to display its Published Date.

    Date Last Updated on WordPress' Twenty Twenty Theme.
    By using a filter we can append the Last Updated date to Date Published.

    By hooking into this filter, we can modify the returned result

    So what we’re building today is a WordPress plugin which:

    1. Checks if we’re actually on a Post Page.
    2. Checks if the Last Modified Date isn’t ‘smaller than’ or equal to the Published Date (which can happen when you schedule a post.)
    3. If the above conditions are met, it takes the Published Date and appends the Date Last Updated in parenthesis.

    And all this in about 50 lines of code. Not bragging. I’m just that good.

    Create a file called daan-date-last-modified.php in WordPress’ wp-content folder and paste the following code in it:

    <?php
    /**
    * Plugin Name: Date Last Modified for WordPress Posts
    * Plugin URI: https://daan.dev/wordpress/date-last-updated-modified/
    * Description: Display 'Last Updated' date along with 'Date Published' in WordPress.
    * Version: 1.0.0
    * Author: Daan van den Bergh
    * Author URI: https://daan.dev
    * License: GPL2v2 or later
    * Text Domain: daan-date-last-modified
    */
    function daan_add_date_last_modified($date_published)
    {
    global $post;
    $post_id = get_queried_object_id();
    // Make sure to only apply this filter on the current post, and not 'recent posts' widgets, archive pages, etc.
    if ($post_id != $post->ID) {
    return $date_published;
    }
    /**
    * get_post_datetime() return datetime-object which can be compared, formatted, modified, etc.
    */
    $modified_datetime = get_post_datetime($post->ID, 'modified');
    $published_datetime = get_post_datetime($post->ID);
    /**
    * Exit if Date Last Modified is equal to or smaller than Date Published.
    */
    if ($modified_datetime <= $published_datetime) {
    return $date_published;
    }
    $text_before_date = __('Updated', 'daan-date-last-modified');
    $before = '(' . $text_before_date . ': ';
    $after = ')';
    $modified_ymd = $modified_datetime->format('Y-m-d');
    $modified_human_readable = $modified_datetime->format(get_option('date_format'));
    $updated_html = $before . sprintf(
    '<time class="entry-date" datetime="%s" itemprop="dateModified">%s</time>',
    esc_attr($modified_ymd),
    esc_attr($modified_human_readable)
    ) . $after;
    return $date_published . ' ' . $updated_html;
    }
    // ATTENTION: The first parameter in add_filter() is the filter's name. This might differ per theme.
    add_filter('the_time', 'daan_add_date_last_modified', null, 1);

    After saving the file, login to WordPress’ administrator area and go to your Plugins screen. You should see a new plugin listed, called Date Last Modified for WordPress Posts. Activate it and you’re done!

    Discussion

    As you can see, it’s quite easy to add the Last Updated Date to WordPress posts in a user-friendly and elegant way. The only thing to keep in mind is, that with so many different themes and theme developers, this approach, while being more elegant, isn’t a one-size-fits-all solution. That’s why most WordPress bloggers and developers choose a more universal (but less appealing) solution: prepend the Date Last Modified to the post’s content. Which did you pick?

    Daan van den Bergh

    Daan van den Bergh is a carefully seasoned web developer. His methods consist of thinly slicing your website’s beef and serve you with a platter of the best performance carpaccio on a bed of rocket — the only thing he’s more passionate about is food. Hire him at ffwp.dev.

    3 thoughts on “How to add Date Last Updated to WordPress Posts”

    1. Hi Dann, I have just tested your date-last-updated-modified “plugin” with Twenty Twenty Version: 1.2 , unfortunatelly it doesn’t work

      I did it all like you have written in your post

      Reply
    2. hello Daan
      thannk you for this plugin
      How would you change to ‘last update” for ARCHIVE pages? and for PAGES? (not for a post)
      I’m using Divi theme, inspect of archive page date show “Aug 24, 2020
      many thanks
      Avi

      Reply

    Leave a Comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.