/**
 * Display a last modified line on updated posts.
 *
 * @param string $content post content.
 *
 * @return string
 */
function dorzki_display_modified_date( $content ) {

  // Create new datetime instances.
  $original = new DateTime( get_the_time( 'Y-m-d' ) );
  $modified = new DateTime( get_the_modified_time( 'Y-m-d' ) );

  // Check if modified date is bigger then created date.
  if( $modified > $original ) {

    $modified_text = sprintf( '<p class="modified_date">%s %s</p>', __( 'The page was last modified on', 'dorzki' ), "<time datetime='{$modified->format( 'c' )}'>{$modified->format( 'd-m-Y' )}</date>" );

    $content = $modified_text . $content;
    
  }

  return $content;

}

add_filter( 'the_content', 'dorzki_display_modified_date' );

Credit: dorzki.co.il

How to use it?

Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php file at the end of the file, just before ?>.

For JavaScript code you will need to add them to your theme’s main JavaScript file.

Having issues with the code?

If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.

Comments

Leave a Reply