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.
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].
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.
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
Thanks for sharing both the plugin based and plugin-free ways. I would go with the plugin-based method 🙂
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 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.