How to Show a WordPress User’s Last Login Date? If your WordPress website has many users, it’s a good idea to keep track of who’s using it. For instance, if you have many administrators, it’s useful to occasionally clean up the user list.
That’s when knowing the last time a user logged in can be handy. In this article, we’ll show you how to show a user’s last login time. You can even organize the list to see who hasn’t logged in recently.
We’ll start by using a free plugin to do this:
Displaying a WordPress User’s Last Login Date with a Plugin
The simplest method to display the last login time of a WordPress user is by using the “When Last Login” plugin. Once you install and activate the plugin, you will notice a new column added to the list of users.
If a user hasn’t logged in after you installed the plugin, it will show “Never” in that column.
Manually Show User’s Last Login Date Code Snippet
If you know how to add code snippets to your site using functions.php or a plugin like Code Snippets, you can use this code to display the user’s last login time. This code does a few things: First, it records the time when a user logs in and stores it in the ‘last_login’ user meta. Then, it adds a sortable ‘Last Login’ column to the user list in the admin dashboard. Additionally, it allows you to show a user’s last login time using a [lastlogin] shortcode. You can even specify a particular user’s ID to show their last login time using the user_id variable, like [lastlogin user_id=2].
<?php //Record user's last login to custom meta add_action( 'wp_login', 'da_capture_login_time', 10, 2 ); function da_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', 'diwakar_academy_user_last_login_column' ); add_filter( 'manage_users_custom_column', ' diwakar_academy _last_login_column', 10, 3 ); function diwakar_academy_user_last_login_column( $columns ) { $columns['last_login'] = 'Last Login'; return $columns; } function diwakar_academy_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', 'diwakar_academy_sortable_last_login_column' ); add_action( 'pre_get_users', 'diwakar_academy_sort_last_login_column' ); function diwakar_academy_sortable_last_login_column( $columns ) { return wp_parse_args( array( 'last_login' => 'last_login' ), $columns ); } function diwakar_academy_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 diwakar_academy_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', 'diwakar_academy_lastlogin_shortcode' );
Once you insert this code snippet, the plugin will start keeping track of users’ login times, and a new column named “Last Login” will appear. If a user hasn’t logged in after you’ve added the code, you’ll see a message saying “No record” in that column.
If you want to know the specific time of login, you can hover your mouse over the time in the column, and you’ll see the complete timestamp.
The great thing is, if you’re comfortable with PHP, you can modify and adjust this feature according to your needs.
This code snippet also enables you to show a user’s last login time on the front end. For instance, in the author box on your website, you can use the [lastlogin] shortcode to let users see when the author was last active.
I hope this simple guide helped you understand how to display a user’s last login in WordPress. If you have any questions, feel free to ask in the comments below.
Related Articles
Leave a Reply