How to Display ‘Last Modified’ Dates on Your WordPress Posts

Home » Snippets » How to Display ‘Last Modified’ Dates on Your WordPress Posts
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Keeping your readers informed about the most recent updates to your posts can make your WordPress site more user-friendly and your content more reliable. One approach to accomplishing this is by displaying the ‘last modified’ date on your posts. This is a clear way to signal to your readers when a piece of content has been updated or corrected. In this article, we’ll guide you through the steps of successfully integrating a ‘last modified’ date on your WordPress posts.

					function wpturbo_post_last_modified() {
    global $post;
    $date_format = get_option( 'date_format' );
    $last_modified = get_the_modified_date( $date_format, $post->ID );
    return "<p class='wpturbo-last-modified'>" . sprintf( __( 'Last modified: %s', 'wpturbo' ), $last_modified ) . "</p>";
}
add_filter( 'the_content', 'wpturbo_post_last_modified' );
				

The code snippet begins with defining a new function – wpturbo_post_last_modified(). The function is used to fetch the last modified date of a particular post and then append this information to the original content of the post.

Let’s take a closer look at each part of the function:

global $post;

The global statement is used to import the global scope of the variable $post into the function. $post is a WordPress global variable that holds all the data related to the current post/page being viewed.

$date_format = get_option( 'date_format' );

The get_option() function is a built-in WordPress function that retrieves the data of a named option. In this case, it retrieves the website’s date format set in the WordPress settings.

$last_modified = get_the_modified_date( $date_format, $post->ID );

The get_the_modified_date() function retrieves the last modified date of the specified post, which in this context is the global post. The date format fetched earlier and the ID of the specific post is passed as arguments to the function.

return "<p class='wpturbo-last-modified'>" . sprintf( __( 'Last modified: %s', 'wpturbo' ), $last_modified ) . "</p>";

Finally, the last modified date fetched is returned wrapped in a paragraph tag having the class ‘wpturbo-last-modified’. sprintf is used to format the string and __() is another built-in WordPress function used for internationalization (i18n) and localization (l10n). It is used here to make the ‘Last modified’ string translatable.

add_filter( 'the_content', 'wpturbo_post_last_modified' );

In the last line of the code, we see the add_filter function. This is a hook provided by WordPress. ‘the_content’ is a WordPress filter that filters the content of the post after it is fetched from the database but before it is printed to the screen. By adding ‘wpturbo_post_last_modified’ function to this filter, we ensure that the last modified date of a post gets appended to the original content every time the content of a post is displayed.

Register an account to save your snippets or go Pro to get more features.