How to Display a Column with Users’ Last Login Date / Time
Here is what we got as a result:

Step 1. Store Last Login Timestamp in User Meta
Before displaying last sign up date or time we have to collect it first, right? WordPress doesn’t collect or store it anywhere by default, so we have to do it on our own.
You have to understand also that there are two types of sign in in WordPress:
- via the default
wp-login.php
form, - or via
wp_signon()
function in the code.
Luckily there is wp_login
action hook which works for both of them!
add_action( 'wp_login', 'misha_collect_login_timestamp', 20, 2 );
function misha_collect_login_timestamp( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
All you have to do is to copy this piece of code to your current theme functions.php
/ custom plugin / child theme’s functions.php
. Only since that moment WordPress begins to collect the UNIX timestamp and store it in wp_usermeta ({db prefix}usermeta) table every time a user signs in on your website.
Step 2. Display a Column on All Users Page
I already have a plenty of columns tutorials on my website like this one about WooCommerce columns or about creating a sortable registration date column. So I am going to be short here.
add_filter( 'manage_users_columns', 'misha_add_last_login_column' );
add_filter( 'manage_users_custom_column', 'misha_last_login_column', 10, 3 );
function misha_user_last_login_column( $columns ) {
$columns['last_login'] = 'Last Login'; // column ID / column Title
return $columns;
}
function misha_last_login_column( $output, $column_id, $user_id ){
if( $column_id == 'last_login' ) {
$last_login = get_user_meta( $user_id, 'last_login', true );
$date_format = 'j M, Y';
$output = $last_login ? date( $date_format, $last_login ) : '-';
}
return $output;
}
As a result of this step we have the following:

Bonus Step. Making the Last Login Column Sortable
I think it is a quite useful bonus step because it allows to find out quickly who logged in recently.
Please note, that there is no rule one for all when you’re making a column sortable, for a custom Users column it can be done the way described below, but it is completely different for CPT columns for example.
add_filter( 'manage_users_sortable_columns', 'misha_sortable_columns' );
add_action( 'pre_get_users', 'misha_sort_last_login_column' );
function misha_sortable_columns( $columns ) {
return wp_parse_args( array(
'last_login' => 'last_login'
), $columns );
}
function misha_sort_last_login_column( $query ) {
if( !is_admin() ) {
return $query;
}
$screen = get_current_screen();
if( isset( $screen->id ) && $screen->id !== 'users' ) {
return $query;
}
if( isset( $_GET[ 'orderby' ] ) && $_GET[ 'orderby' ] == 'last_login' ) {
$query->query_vars['meta_key'] = 'last_login';
$query->query_vars['orderby'] = 'meta_value';
}
return $query;
}
More examples

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Nice, thanks !
it’s very useful
Thanks for the tuto!
I suggest you go one step further. If a user has not logged in for x days, an email is sent from WordPress with a question. Or better yet you can connect to Mailchimp. It’s just an idea.
Thanks again
Cheers
Hey Carlos,
Great idea, thanks!
Thanks so much for this tutorial. I’d also love to know how to take this to the next step and send a wp_mail() if a user has been inactive after a certain amount of time, or trigger a mailchimp hook etc…
Hello Misha,
We appreciate your this beautiful article. in this article have issue with ending function brackets. function name below.
1. misha_add_last_login_column.
2. misha_sort_last_login_column.
Just added in my project.
Thanks –
Hello,
Fixed, thanks! 🙃
Great post!
In your bonus code there are some extra
after your functions’ closing
. Easy enough to fix but I figured I’d let you know!
Thank you very much! Fixed! 🙃