"Since Your Last Visit" Dashboard Widget
Adds a dashboard widget listing what happened since you last logged in to the admin: new posts by others, new comments and new users, so you catch up in seconds.
Opens WPIDE on
Code
add_action( 'admin_init', function () {
if ( wp_doing_ajax() ) {
return;
}
$user_id = get_current_user_id();
$last = (int) get_user_meta( $user_id, 'wpide_last_admin_visit', true );
// Move the "previous visit" marker forward after an hour away.
if ( $last < time() - HOUR_IN_SECONDS ) {
update_user_meta( $user_id, 'wpide_prev_admin_visit', $last ? $last : time() );
update_user_meta( $user_id, 'wpide_last_admin_visit', time() );
}
} );
add_action( 'wp_dashboard_setup', function () {
wp_add_dashboard_widget( 'wpide_since_last_visit', 'Since your last visit', function () {
$since = (int) get_user_meta( get_current_user_id(), 'wpide_prev_admin_visit', true );
if ( ! $since ) {
echo '<p>Welcome! Activity will appear here from your next visit.</p>';
return;
}
$after = gmdate( 'Y-m-d H:i:s', $since );
$posts = get_posts( array(
'post_type' => array( 'post', 'page' ),
'posts_per_page' => 8,
'no_found_rows' => true,
'author__not_in' => array( get_current_user_id() ),
'date_query' => array( array( 'column' => 'post_date_gmt', 'after' => $after ) ),
) );
$comments = get_comments( array( 'status' => 'approve', 'count' => true, 'date_query' => array( array( 'column' => 'comment_date_gmt', 'after' => $after ) ) ) );
$pending = get_comments( array( 'status' => 'hold', 'count' => true, 'date_query' => array( array( 'column' => 'comment_date_gmt', 'after' => $after ) ) ) );
$users = current_user_can( 'list_users' ) ? count( get_users( array( 'fields' => 'ID', 'date_query' => array( array( 'column' => 'user_registered', 'after' => $after ) ) ) ) ) : 0;
printf( '<p>Since %s ago:</p><ul>', esc_html( human_time_diff( $since ) ) );
printf( '<li><strong>%d</strong> new comments%s</li>', (int) $comments, $pending ? ' (<a href="' . esc_url( admin_url( 'edit-comments.php?comment_status=moderated' ) ) . '">' . (int) $pending . ' awaiting moderation</a>)' : '' );
if ( current_user_can( 'list_users' ) ) {
printf( '<li><strong>%d</strong> new users</li>', (int) $users );
}
printf( '<li><strong>%d</strong> new posts and pages by others</li></ul>', count( $posts ) );
if ( $posts ) {
echo '<ul style="margin-left:1em;list-style:disc">';
foreach ( $posts as $post ) {
printf( '<li><a href="%s">%s</a> <small>by %s</small></li>', esc_url( get_permalink( $post ) ), esc_html( get_the_title( $post ) ), esc_html( get_the_author_meta( 'display_name', $post->post_author ) ) );
}
echo '</ul>';
}
} );
} );
Notes
Your last visit is recorded at most once an hour, so short breaks do not reset the list.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.