• hello,

    I want to display last modified date and time for entire website at footer area whenever anytime any page or post is modified.

    This I want to do with Function Reference/the modified time of codex.

    I don’t want to use any plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • My idea is to add an option to store the last modified time and change it whenever a post/page is updated.

    We can use the save_post filter to do this.
    Basically we check if the creation date equals to the modified date, if yes then the post should be newly created, if not, we update the option with the new array of latest modified date and latest modified post ID.

    add_action( 'save_post', 'save_last_modified_date', 10, 1 );
    function save_last_modified_date( $post_id ) {
    	 // If this is just a revision, do nothing.
    	if ( wp_is_post_revision( $post_id ) )
    		return;
    
    	$created_date = get_the_date( 'U', $post_id );
    	$modified_date = get_the_modified_date( 'U' );
    
    	$attr = get_option( 'lastModified', array() );
    
    	if ( $created_date !== $modified_date ) {
    
    		$attr['date'] = $modified_date;
    		$attr['post_id'] = $post_id;
    
    		update_option( 'lastModified', $attr );
    	}
    
    }

    Then you can get the last modified date across the whole site by calling:

    $lastModifed = get_option( 'lastModified' );
    if ( $lastModifed ) {
      $date =  $lastModifed['date];
      $post_id = $lastModifed['post_id];
    }
    Thread Starter RKM Shilpavidyalaya

    (@rkm-shilpavidyalaya)

    Dear sir,

    Glad to receive your reply.

    Where should I add this code given above to get the display of last modified on footer area.

    I am new person on code customization. Please help me step by step.

    Add the 1st piece of code to your functions.php.
    The 2nd piece is the code to display the date, add it to where you want, let’s say on footer area, it usually goes to footer.php. Here is an example (I didn’t test it yet, use it on your own risk ;)):

    $lastModifed = get_option( 'lastModified' );
    if ( $lastModifed ) {
      $date =  $lastModifed['date];
      $post_id = $lastModifed['post_id];
    
      echo "Last Modified: ".date_format($date, 'Y-m-d H:i:s');
    }
    Thread Starter RKM Shilpavidyalaya

    (@rkm-shilpavidyalaya)

    Dear newbiesup

    thank you for your help, but the code you specified did not work.

    It’s weird…it worked on my local environment….let me check

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display last Modified date and time for entire website without plugin’ is closed to new replies.