WordPress doesn’t track when users last logged in. If you manage a site with multiple authors, editors, or administrators, that makes it hard to audit who’s still active and who should be removed.
Adding a last login date to your WordPress user list solves this. You can spot inactive accounts, clean up old users, and keep tighter control over who has access to your site. This is especially useful for security: dormant admin accounts with weak passwords are easy targets for attackers.
Below are two ways to add last login tracking: a free plugin (quickest) and a custom code snippet (more flexible).
Method 1: Show Last Login with a Plugin
The easiest way to display a WordPress user’s last login time is the When Last Login plugin. It has over 50,000 active installs and is free.
After installing and activating the plugin, you’ll see a new “Last Login” column on the Users screen (Users > All Users).
Users who haven’t logged in since the plugin was activated will show “Never.” The plugin can only record logins going forward; it can’t retroactively detect past login dates.
The plugin also records IP addresses (with GDPR-friendly anonymization options) and lets you clear login records if needed.
Method 2: Show Last Login with a Code Snippet
If you prefer not to install a plugin, you can add last login tracking with a custom code snippet. Add this to your theme’s functions.php file or use the Code Snippets plugin.
This snippet does three things:
- Records the timestamp each time a user logs in (saved as
last_loginuser meta) - Adds a sortable “Last Login” column to the Users admin screen
- Creates a
[lastlogin]shortcode you can use anywhere on your site, including author bios. Use[lastlogin user_id=2]to show a specific user’s last login.
| <?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 the snippet, user logins will be recorded and a new “Last Login” column appears in the Users list. Users who haven’t logged in since you added the code will show “No record.”
Hover over the relative time (e.g., “2 hours ago”) to see the full timestamp.
Since this is custom PHP, you can modify the date format, change the column position, or adjust the output to fit your needs. The [lastlogin] shortcode also works on the frontend, so you can display “last active” timestamps in author boxes or member profiles.
Which Method Should You Use?
Use the plugin method if you want a quick setup with no code. Use the code snippet if you want more control, don’t want an extra plugin, or need the frontend shortcode. Both approaches store the same data (a timestamp in user meta) and won’t affect site performance.
If you need full login history (not just the last login), consider a dedicated activity log plugin like WP Activity Log, which tracks every login, logout, failed attempt, and content change.
Frequently Asked Questions
Can I See Login Dates for Users Who Logged In Before I Added Tracking?
No. Both the plugin and code snippet only record logins that happen after activation. There’s no way to retroactively pull past login dates because WordPress doesn’t store this data by default. Existing users will show their last login the next time they log in.
Does Tracking Last Login Slow Down My Site?
No. Both methods write a single timestamp to the wp_usermeta table during login. This adds negligible overhead, even on sites with thousands of users.
Can I Sort Users by Last Login Date?
Yes. Both the plugin and the code snippet add a sortable column to the Users screen. Click the “Last Login” column header to sort users by their most recent (or oldest) login, making it easy to find inactive accounts. If you need to change a username while you’re auditing accounts, we have a guide for that too.
That’s all it takes to add last login tracking to WordPress. Whether you use the When Last Login plugin or the custom code snippet, you’ll have a sortable “Last Login” column in your Users list within minutes.






12 Responses
May I know what wp theme you are using? I love the layout and speed.
Your site is fast, how to archive this?
This is a fantastic article. You are easily described how to show a user's last login date in WordPress. It is really helpful. Thanks
Hi Andy,
Thanks for sharing both the plugin based and plugin-free ways. I would go with the plugin-based method 🙂
Cheers!
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
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
Since it’s stored in user meta you can just grab the field and put it anywhere you’d like.
$last_login = get_user_meta( $user_id, ‘last_login’, true );
This code caused a critical error at my dashboard 🙁
Any idea how I can solve this?
Nice hack, Andy and your code is working nicely on my site without any error. Thanks a lot for sharing.
Thank you Andy !
exactly what I was looking for !
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 !
Dear ALL Beloved Friends,
I am using code snippets plugin
Would you mind telling me where to add this code ? is it on a body ? or File editor or else?
or different plugin ,feel free to suggest
Thanks
PIPE