How to Display a Column with Users’ Last Login Date / Time

Recently I was needed this functionality for my own website and I decided to share the process how I implemented it with you.

/4 comments

Here is what we got as a result:

Create sortable users last login column in WordPress admin dashboard, Users page.
As you can see here, the column is sortable, but it is an optional step.

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:

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:

Column with users Last Login Date on All Users page
As you can see, this column is not sortable… yet.

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

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 4

Leave a comment

php js HTML CSS Code

I will only use your personal information to contact you. Privacy Policy