Get Qi Theme with 150 superbly designed WordPress demos

Get Qi Theme
Qi Theme
Sign up for our newsletter and be the first to get all the latest Qode news and updates.
BACK TO TOP

How to Show a User’s Last Login Date in WordPress

How to Show a User’s Last Login Date in WordPress

With all manner of commercial and social activity moving online with growing volume and intensity, it should come as no surprise that online security is a growing concern. It is also no surprise creating high-quality content for the internet is growing ever more complex. Even a small website might have several people behind it, working diligently towards ensuring a good user experience.

This is why you may be interested in knowing the exact time each user was logged into your website and in ways to display a user’s last login date and time. This is how both you and the user can keep track of any suspicious activity and make corrections and changes when needed. It will also make it easier for you to monitor the time your admins and contributors spend working on the website. There are two easy ways to make that happen: using a plugin and editing your theme files.

Qode Themes: Top Picks
Bridge New Banner
Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme
Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme
Startit

Fresh Startup Business Theme

How to Show a User’s Last Login Date Using a Plugin

The simplest way to show a user’s last login date is using a plugin. We will be using a free plugin called When Last Login in order to display a user’s last login time in the back end section of your website: visible to admins and other users with back end access. In order to use it to show the last login date of any of your users, you need to install and activate this plugin.

Having done that, you need to navigate to the plugin’s settings in order to configure it. You will find the settings on the left hand-side menu of your WordPress dashboard, under When Last Login/Settings.

When Last Login Settings

You will find that the plugin’s settings have two sections: Options and Tools. Under Options, you will find two checkboxes. The Record user’s IP address checkbox will, when checked, cause your website to record users’ IP addresses. To avoid falling afoul of GDPR, it will also anonymize them. Checking Enable “All Login Records” will record all logins. Check them both.

Insofar as the Tools sections are concerned, you will find the options to Clear old logs, Clear all logs, and Clear all IP Addresses. You can use them in order to clear the logs in case you no longer need them. Once you have everything set up, click the Save Settings button.

Options and Tools

In order to see when your users have last logged in, navigate to the Users/All Users panel from your WordPress dashboard.

All Users

You will find that the plugin is now displaying the last login time for each user. Since we only have one user (admin), that’s what we are seeing.

View Last Login

How to Show a User’s Last Login Date Using Code

You can also use code to display the last login time for each user on the front end side. If you do not come from a technical background, we strongly suggest you use the plugin method as described above. We also strongly suggest you backup everything before making any changes to your theme files.

In order to show a user’s last login you will need to change your theme’s functions.php file. To access it, navigate to Appearance/Theme Editor from your WordPress dashboard.

Theme Editor

You also need to select the functions.php file from the right hand-side menu.

Functions PHP

Next, you need to copy the following code and paste it into the file, making sure not to disturb any other code:

/**
* 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');

When you have done that, click the Update File button.

Paste Code

The next thing you need to do is log out and log back in. The changes in code will only then take effect.

Once you have done that, you will have gained the use of the [lastlogin] shortcode. We will show you how to use it as part of a post, but you can use it anywhere you want on your website.

Say we want to show when a particular user was last seen on the website, and say we want to incorporate it into the text of a post, or a byline. We will begin by creating a post and giving it a title. Next, to add the body of the post, we will need a Classic block. To add a Classic block, click the plus icon and select the block from the menu.

Classic Block

We will now add some lorem ipsum text and the Admin‘s signature. The [lastlogin] shortcode is in the place where we want to show the last login time.

Add Text

Once you have everything just right, click Publish. The shortcode will now display the time of your last login.

You may not want this particular datum to be accessible to all visitors of your website at all times. Not to worry: you can always turn your post or page into a private page.

Last Login Preview

In Conclusion

As you can see, displaying a user’s last login date is not a big deal and it can save you a lot of trouble when determining who accessed what and when or whether a user account has been hacked or not, or maybe if you just want to monitor the time your administrators and contributors spend working on your website. Regardless of your motivation, now you know how it’s done.

Post your comment

Comments0