Recently, one of our readers asked us how to show a user’s last login date in WordPress. You may need this if you wanted to add an author activity box on your WordPress site. In this article, we will show you how to display the user’s last login date in WordPress.
Method 1: Showing a User’s Last Login Date in WordPress Admin Area
This method is easier, but it will only show a user’s last login date inside “WordPress admin area”.
The first thing you need to do is install and activate the “WP Last Login” plugin.
Upon activation, you need to visit the ‘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 was activated so that it could capture 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.
Simply add this code to your theme’s “functions.php” file or a “site-specific plugin.”
If you’re new to adding code, then please read this guide on pasting the code from the web.
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. Before you want to test the plugin, you need to log out of WordPress and then log in again.
You can then display this meta key value using the
[lastlogin]shortcode in your WordPress posts and widgets.
If you want to show last login information in your child theme, then you can add this code:
As you would notice that this code displays relative date and time, i.e. ‘2 hours ago’ instead of full date and time. If you want to display the full date and time, then locate this line in the code above:
Now replace it with this line:
The ‘M j, Y h: I a’ part in this code is called date and time format string. If you want to change how this code displays date and time, then 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 the user’s last login date in WordPress.