Displaying a “Last Modified” Date in WordPress — Updated for Modern WP

On more than one occasion, I’ve started reading an article only to realize partway through that it was published years ago. While many authors update their content over time, readers often can’t tell whether the information is still current — so they move on, even if the article is perfectly relevant.

To solve this, I put together a small snippet of code that I’ve used across several sites to clearly show readers when an article was last updated. I originally wrote this back in 2009 for WordPress 2.8, and the logic still holds up — but the code has been updated below to reflect modern WordPress standards.


How It Works

The code works in two steps. First, it checks the article’s original publication date and compares it to the date it was last modified. If those two dates match — meaning the content has never been edited — no modified date is shown. But if the dates differ, the last modified date is displayed automatically.


The Original Code (WordPress 2.8, 2009)

<?php $origdate = get_the_time(); $moddate = get_the_modified_time(); if ($origdate != $moddate): echo '<strong>Modified: </strong>'; {the_modified_date();}; endif; ?>

This worked well at the time, but there are a few things worth updating for reliability and security.


The Updated Code (Modern WordPress)

<?php
$orig_date = get_the_date( 'U' );
$mod_date  = get_the_modified_date( 'U' );

if ( $orig_date !== $mod_date ) {
    echo '<p class="last-modified"><strong>Last Modified: </strong>'
        . esc_html( get_the_modified_date() )
        . '</p>';
}
?>

What Changed and Why

Timestamp comparison — The updated code uses the 'U' format parameter to return a Unix timestamp (a plain number) for both dates, making the comparison far more reliable. The original compared formatted date strings, which could occasionally differ due to timezone or formatting inconsistencies.

get_the_modified_date() instead of the_modified_date() — The original used the_modified_date(), which echoes output directly. The modern preference is to use the get_ version to retrieve the value first, then echo it yourself — giving you more control over the output.

esc_html() — Wrapping the output in esc_html() escapes any potentially unsafe characters before printing. This is standard security practice in WordPress development today.

CSS class — The updated version wraps the output in a <p> tag with a last-modified class, making it easy to style through your theme’s stylesheet rather than relying solely on inline HTML tags.


How to Use It

The placement is the same as before — drop the code into your single.php file wherever you’d like the modified date to appear. If you’re running a block-based theme, you may need to add it to a custom template part instead, as classic single.php files are handled differently in the block editor era.