0

How to display the last time the site was updated(the last time either a post or a page was created/ modified/deleted) in the header of my WordPress site?

1

1 Answer 1

3

Two ways at least:

Method 1

If you happy to accept the latest of all the wp posts 'last modified' as the date that the site was last updated, then you could add a function to get the latest of the 'post_modified' in the posts table and add that to your header.php either in the template OR add to header via a header action.

something like

$query = "SELECT MAX(post_modified) as wplast FROM `wp_posts` ;
$last_updated = $wpdb->query( $query );
echo $last_updated;

or

Method 2

if you would like a broader definition of last updated, then you'd need to add code to record that - ie Add an 'action' on every wordpress action that you think should count as an update. Eg: delete_post, save_post, publish_page etc See https://codex.wordpress.org/Plugin_API/Action_Reference#Post.2C_Page.2C_Attachment.2C_and_Category_Actions_.28Admin.29 and https://developer.wordpress.org/reference/functions/add_action/

something like:

function record_time () {
    $datetime = date("Y-m-d H:i:s");
    update_option('time_last_updated',$datetime );
}
add_action( 'save_post', 'record_time', 10, 3 );

The in the header or header filter/action:

echo get_option('time_last_updated');
1
  • 1
    For performance and maintability reasons I would recommend the second option
    – Tom J Nowell
    Commented Jul 10, 2018 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.