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.
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.
How do I know which Filter to use?
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:
- Right-click on its published date and click ‘Inspect’. Some browsers call it ‘Inspect Element’.
- A window, called DevTools, pops up with information about the entire website and more importantly, the Published Date element.
- Within the highlighted lines, take note of the value of the
class
attribute. - 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')
orthe_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.
By hooking into this filter, we can modify the returned result
So what we’re building today is a WordPress plugin which:
- Checks if we’re actually on a Post Page.
- Checks if the Last Modified Date isn’t ‘smaller than’ or equal to the Published Date (which can happen when you schedule a post.)
- 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?
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
Hi Peter,
Did you actually update a published post after the date it was published? Because that’s the date that plugin will show… I just tested it again on my testing environment, and it works: https://imgur.com/a/KqEDxp1
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
thank you very much for this useful solution!
Sure! No problem 🙂
Thank you! Is there a way to modify the date format? I would like to have the month without capital.
Yes there is! On line 40 the format is defined as ‘Y-m-d’. You can change this to anything you like, take a look at PHP’s documentation: https://www.php.net/manual/en/datetime.format.php
I’d like to see this code where (1) it doesn’t spit out the HTML class assignment and (2) how to get the “Updated” date on a new line (below the published date). I’ve tried everythig and I can’t make these things happen.