Theme & Front End PHP Beginner

Show a “Last Updated” Date on Posts

Add "Last updated on…" above posts that were changed at least a day after publishing.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 18 lines
add_filter( 'the_content', function ( string $content ): string {
	if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
		return $content;
	}

	// Skip posts that were only tweaked on the day they were published.
	if ( (int) get_the_modified_time( 'U' ) - (int) get_the_time( 'U' ) < DAY_IN_SECONDS ) {
		return $content;
	}

	$notice = sprintf(
		'<p class="nahnu-last-updated">Last updated on <time datetime="%s">%s</time></p>',
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	return $notice . $content;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Readers trust recent information, and most themes only show when a post was first published. Showing the update date on refreshed articles tells readers the content is current.

How it works

On single posts, the filter compares the publish and modified times. If the post was changed at least a day after publishing, a short line with the modified date is added above the content, in a &lt;time&gt; element with a machine-readable date.

The checks for in_the_loop() and is_main_query() stop it appearing in widgets, related posts or excerpts.

Things to watch

  • Fixing a typo counts as an update. Only show this if you mostly change posts meaningfully.
  • Some themes already show the modified date. Check first so it doesn’t appear twice.

How to test it

Edit an old post and update it. Viewing the post should now show “Last updated on” with today’s date.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only