Add "Last updated on…" above posts that were changed at least a day after publishing.
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;
} );
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.
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 <time> 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.
Edit an old post and update it. Viewing the post should now show “Last updated on” with today’s date.