How to Display the Last Updated Date in WordPress Posts With PHP Code?

WordPress is one of the most favoured CMS in the market and rightly so, the extent to which it is customizable & flexible is breathtaking.

Not only do plugins make life easier but as it just so happens you can also leverage PHP code automation to implement features that support SEO too.

In this particular post, I will explain how you can leverage PHP Code to automate the display of the last Update date on your WordPress blog post.

Why the need to display the Last Updated Date on Content Landing Pages?

This thought may occur in your mind why display the last updated date on the blog post to begin with 🤷‍♂️

  1. Displaying the latest updated date prompts users that they are consuming the latest data. For example, let’s say the user is on a landing page titled “e-commerce SEO Strategies” Ideally it would be reassuring if the reader is prompted that he is reading an updated landing page.

  2. There is no data to back this, but there are speculations that suggest that Google would want to see content refresh to keep the page fresher in the SERPs seeing the last updated date is one way for them to figure it out.

Here is the PHP Code that you need to place at the bottom of the functions.php file for this automation to work

					function my_last_updated_date( $content ) {
    // Check if it's a blog post
    if ( is_single() ) {
        $u_time = get_the_time('U');
        $u_modified_time = get_the_modified_time('U');
        if ($u_modified_time >= $u_time + 86400) {
            $updated_date = get_the_modified_time('F jS, Y');
            $updated_time = get_the_modified_time('h:i a');
            $custom_content .= '<p class="last-updated entry-meta">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';
        $custom_content .= $content;
        return $custom_content;
    // For other pages (not single blog posts), return the original content
    return $content;
add_filter( 'the_content', 'my_last_updated_date' );

Disclaimer: It is advisable to create a backup of your website before making this change to functions.php so if things go south then you immediately restore the latest backup.

This PHP Code will automatically update the last update date on all your blog posts that were updated after publishing it. This line below makes sure that this automation gets applied only to Blog post pages & not all landing pages.

					if ( is_single() ) {

Here is an example of how it displays the last updated date on the blog posts.

wordpress last updated date blog posts

Hope this helps 😉

Leave a Comment