How to Show a User’s Last Login Date in WordPress (With and Without a Plugin)

If you have a WordPress site with a lot of users it can be important to audit who is actually using the site. For example if you have a lot of admins you periodically may want to trim down users.

This is where having a WordPress user’s last login date can be helpful. In this post we’ll be enabling a user’s last login time and you’ll be able to sort it so you can see who hasn’t logged in for a while.

First we’re going to be achieving this with a free plugin:

Displaying a WordPress User’s Last Login Date with a Plugin

The easiest way to show a WordPress user’s last login time is to use the When Last Login plugin.

After installing and activating the plugin you’ll see a new column in the user list.

User’s last login in the WordPress user list

If a user hasn’t logged in since you’ve added the plugin you’ll see “Never”.

Manually Show User’s Last Login Date Code Snippet

If you’re familiar with adding code snippets to your site via functions.php or the code snippets plugin you can use this to add the user’s last login time.

This code snippet does a few things, first it records the time a user logs in and saves it in the ‘last_login’ user meta. It also adds a sortable ‘Last Login’ admin column to the user list in the admin dashboard. Lastly it allows you to display the user’s last login via a [lastlogin] shortcode. The shortcode also lets you show a specific user’s last login by using the user_id variable [lastlogin user_id=2].

<?php
//Record user's last login to custom meta
add_action( 'wp_login', 'smartwp_capture_login_time', 10, 2 );
function smartwp_capture_login_time( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
//Register new custom column with last login time
add_filter( 'manage_users_columns', 'smartwp_user_last_login_column' );
add_filter( 'manage_users_custom_column', 'smartwp_last_login_column', 10, 3 );
function smartwp_user_last_login_column( $columns ) {
$columns['last_login'] = 'Last Login';
return $columns;
}
function smartwp_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 = 'M j, Y';
$hover_date_format = 'F j, Y, g:i a';
$output = $last_login ? '<div title="Last login: '.date( $hover_date_format, $last_login ).'">'.human_time_diff( $last_login ).'</div>' : 'No record';
}
return $output;
}
//Allow the last login columns to be sortable
add_filter( 'manage_users_sortable_columns', 'smartwp_sortable_last_login_column' );
add_action( 'pre_get_users', 'smartwp_sort_last_login_column' );
function smartwp_sortable_last_login_column( $columns ) {
return wp_parse_args( array(
'last_login' => 'last_login'
), $columns );
}
function smartwp_sort_last_login_column( $query ) {
if( !is_admin() ) {
return $query;
}
$screen = get_current_screen();
if( isset( $screen->base ) && $screen->base !== '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;
}
//Add [lastlogin] shortcode
function smartwp_lastlogin_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'user_id' => false,
), $atts, 'lastlogin' );
$last_login = get_the_author_meta('last_login', $atts['user_id']);
if( empty($last_login) ){ return false; };
$the_login_date = human_time_diff($last_login);
return $the_login_date;
}
add_shortcode( 'lastlogin', 'smartwp_lastlogin_shortcode' );

After adding this code snippet user’s logins will be recorded and you’ll see a new last login column. If a user hasn’t logged in since adding it you’ll se a “No record” message.

If you need to know the exact login time you can also hover over the time for the full timestamp.

Best of all if you are familiar with PHP you can easily customize all aspects of this functionality.

This code snippet also added the ability to display a user’s last login on the frontend. For example in your site’s author box you can include the [lastlogin] shortcode for users to see when the author was last online.


I hope this quick guide was useful on showing you how to display a user’s last WordPress last login. If you have any questions let me know in the comments below.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

11 Responses

  1. Hey, I added the code and it works well, but is there a way to show each user (like current user) his last login date after he logs in to the site?
    thanks

    1. Thanks for the script.
      I have an observation. All users sees the last login of the admin (author).
      Is there a way for each user to see their own last login?

      Please help. Thanks

  2. hi Andy

    thanks for sharing !

    any idea how to maje the whole thing working alsi when the user check the “remember me” option ? (there is no login form here, so the time is not saved)

    thanks again !

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.