WordPress is a popular platform for creating websites, and it tracks a lot of data. It doesn’t track and display a user’s last login date, though. This information can be invaluable for website administrators, security purposes, and creating engagement strategies.
To show the user’s last login date, you’ll need to use a WordPress plugin or add a custom code. If you’re not sure how to get started with either of these methods, then don’t worry.
We’ve helped thousands of users get started with WordPress and add custom functionalities to their sites. After testing multiple plugins, we found WP Last Login to be the best option. If you want more control, then we’ve also created a code snippet you can add to your website.
In this article, we will show you how to show a user’s last login date in WordPress.
Why Display User’s Last Login Date in WordPress?
Adding a user’s last login column to your WordPress dashboard and front end helps keep track of when authors, admins, editors, contributors, and other users are logging in and out of your WordPress site.
This is extremely useful for securing your website from any suspicious activity and cyber threats.
For instance, let’s say your site is exposed to a vulnerability caused by a WordPress plugin installed by a user. You can use the last login date to track users and then quickly fix the problem.
Similarly, showing the user’s last login date makes it easier to monitor the time admins, contributors, authors, and others are spending working on your site. You can use this on a multi-author site and track the time spent by each user.
That said, there are 2 ways to show the user’s last login date in WordPress. First, you can use a plugin to show the login date inside the WordPress admin panel. Second, you can manually show the last login date using a custom code snippet.
Simply click the links below to jump ahead to your preferred section:
Method 1: Showing a User’s Last Login Date in WordPress Admin Area
You can easily show the last login date using the WP Last Login plugin. This method is easier, but it will only show a user’s last login date inside the WordPress admin area.
The first thing you need to do is install and activate the WP Last Login plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
The best thing is that the plugin works out of the box, and there are no additional settings you have to worry about.
Upon activation, you need to visit the Users » All Users page in the admin area. You will notice a new column showing each user’s last login date.
At first, it may show ‘never’ for all users. That’s because a user needs to log in since the plugin has been activated so that it can capture the last login date and store it.
Method 2: Manually Show User’s Last Login Date in WordPress
This method allows you to display a user’s last login date anywhere on your WordPress site.
However, it requires adding custom code to your theme’s functions.php file or a site-specific plugin. We don’t recommend editing the functions.php file directly because the slightest mistake can break your website.
An easier way of adding custom code to your site is by using the WPCode plugin. It is the best code snippet plugin for WordPress that helps manage, organize, and insert custom code anywhere on your site without breaking anything. Plus, it also has an AI code generator that will create custom code for you.
First, you’ll need to install and activate the WPCode plugin. If you need help, then please see our guide on how to install a WordPress plugin.
Note: There is also a free WPCode plugin that you can use for this tutorial. However, upgrading to the premium plugin will give you access to a cloud-based code snippet library, smart conditional logic, and more.
Upon activation, you can go to Code Snippets » + Add Snippet from the WordPress dashboard.
Next, simply hover over the ‘Add Your Custom Code (New Snippet)’ option and click on ‘Use snippet’.
After that, you can copy the following code and add it into the Code Preview area:
<?php
/**
* Capture user login and add it as timestamp in user meta data
*
*/
function user_last_login( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
/**
* Display last login time
*
*/
function wpb_lastlogin() {
$last_login = get_the_author_meta('last_login');
$the_login_date = human_time_diff($last_login);
return $the_login_date;
}
/**
* Add Shortcode lastlogin
*
*/
add_shortcode('lastlogin','wpb_lastlogin');
?>
This code adds the last login as a meta key.
Each time a user logs in, it saves the time as a meta key value.
After entering the code, you will also need to enter a title for your custom code and select the ‘Code Type’ as PHP Snippet.
From here, you can scroll down and choose where to insert the code.
For this tutorial, we will use the default method and keep it to the ‘Auto Insert’ method. This way, the custom code will run everywhere on your website.
Once you are done, simply scroll back to the top.
Go ahead and activate your custom code snippet and click the ‘Save Snippet’ button.
If you are new to adding code, then please read this guide on pasting code from the web.
You can then display the last login date value using the[lastlogin]
shortcode in your WordPress posts and widgets.
Simply edit a post or page and go to the content editor.
From here, add a Shortcode block where you want to show the last login date and paste the shortcode.
Similarly, if you want to show the last login information in your child theme, then you can add this code instead:
<?php echo 'Last seen: '. do_shortcode('[lastlogin]') .' ago'; ?>
Before you test the plugin, you will need to log out of WordPress and then log in again.
Then, you should visit your WordPress website to see the login date in action.
As you will notice, this code displays the relative date and time, ‘2 hours ago’ instead of the full date and time.
If you want to display the full date and time, then you need to find this line in the code above:
$the_login_date = human_time_diff($last_login);
Now replace it with this line:
$the_login_date = date('M j, Y h:i a', $last_login);
The ‘M j, Y h:i a’ part in this code is called the date and time format string. If you want to change how this code displays the date and time, then just check out our guide on how to change the date and time format in WordPress.
We hope this article helped you learn how to show a user’s last login date in WordPress. You may also want to see our guide on how to add an author info box in WordPress posts and the ultimate guide to WordPress security.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Babak Fakhamzadeh
It is worth being aware of that Wordfence stores the last login date as a meta field.
WPBeginner Support
Thanks for sharing that current feature for those using Wordfence.
Admin
Fredrick Arije
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
WPBeginner Support
For showing individual last logins we would recommend using the plugin method as a manual code method would be more advanced than what we would recommend for beginners.
Admin
Bobby
Hello,
It works great but the time stamp when the line is changed for date/time still shows a 7 hour time difference?
How can I make the last login time show Pacific Standard Time. That is my timezone.
Please advise, thanks.
Cheers!
Nobin
Thanks For Share That was most useful trick