How to Show Users Last Login Time in WordPress

Do you want to show visitors the last time that they logged into your WordPress website? It is an interesting statistic for them to know. You may also need to add this feature if you are interested in adding an author activity box to your website.

Adding the last login time is easy because WordPress already records that information, the visitors just can’t see it.

You may be wondering exactly why someone would care enough to ask for the ability to see their last login to your site. There are as many answers to that question as there are people. But if your visitors request it or expect it as a feature, why not make it available to them?

Today, I will demonstrate how to show the users last login in WordPress using two methods. First, with a plugin, and then by adding some code to the WordPress functions.php file.

Why Display the User’s Last Login Date in WordPress?

If your visitors have asked for it, that should be enough reason to add the feature to your website. The ability to see the last login date is important in families who share a computer. Imagine siblings who log on to each other’s accounts to prank each other.

If one can see that the other logged in 10 minutes ago, they can probably guess that their sibling was up to no good in the account.

There are other less obvious or common reasons. For instance, it may be useful when you log into an account after being away for a few months, or even years. Many people like to think back or reflect on what they have done during the time that’s passed when they see that last login date.

Okay, maybe that one is a little philosophical or introspective, but you get the idea. 🙂

Let’s get to our first method.

How to Show Users Last Login Time in WordPress Using a Plugin

First, we’re going to show the users last login in WordPress using the When Last Login plugin. Using a plugin is quick, easy and a good option if you are not comfortable adding code to WordPress files (which I’ll show you how to do in a minute).

In the left column navigation mouse over the “Plugins” link and click the “Add New” link.

Add New

In the “Search plugins…” box, enter “When Last Login.”

Search for When Last Login in the available search box.

Click the “Install Now” button and activate the plugin for use.

click the "Install Now" button

In the left column navigation mouse over the “When Last Login” link and click the “Settings” link.

click on When Last Login and select the Settings option.

Since this is the first time you are using the plugin, there isn’t much to do. Users will not see the last login date until they log in after you have activated the plugin.

The plugin should work right out of the box, but if you want to test it, you can create a test account and log in to your website.

The only setting available is the ability to track your visitors’ IP addresses. You can select that if it interests you, but it is not required for the plugin to work. And there are already systems in place to track and block IP addresses.

How to Edit WordPress Files to Show Users Last Login Time

For the second method, we’re going to edit the WordPress functions.php file in the theme you’re using. Going this route requires a little more effort, but it has the advantage of giving you the ability to place the last login notification wherever you’d like it to appear on the site (using shortcode).

It’s also important to note that using a child theme in this regard is perhaps the best idea. This helps prevent the manual code from being lost should your theme have an update.

For this tutorial, we’re going to use cPanel, which is provided by your web hosting provider. But you can also edit the functions.php file using an FTP program if you don’t have cPanel.

In cPanel, click the File Manager option.

Click on the File Manager option.

To find your theme’s functions.php file:

  • Click the public_html directory. This is located in the root folder.
  • Click the wp-content folder. In this section, you’ll find the nuts and bolts of WordPress.
  • Click the themes folder.
  • Click the folder of the theme you are currently using. Remember, using a child theme is beneficial in this regard.
  • Right-click the functions.php file and select the “Edit” option. If you’re using a Mac, hold down the CMD key and click. You can also click the “Edit” function from cPanel’s top tool bar.

Right-click the functions.php files and select the edit option.

Copy the following code and paste it into the functions.php file:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]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 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’,’lastlogin’);[/ht_message]

Click the “Save Changes” button.

Click on the "Save Changes" button.

Now all you have to do to display the last login notifications is add the following shortcode wherever you want the notification to appear:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ][lastlogin][/ht_message]

That’s all there is to it. You have successfully added the user’s last login time in WordPress using either the plugin or coding method. Remember, if you use the coding method, you will have to edit the functions.php file in the new theme to get the functionality back.

Also, keep in mind that a theme update could overwrite your changes to fucntions.php, so I recommend using a child theme any time you make a change to a theme’s files. That way, you can update the “parent” theme and not lose your changes.

Give Your Users a Reason to Log in Everyday

It’s great that you’re now showing the users how often they are logging in, but you also need to make sure you are giving them a reason to log in. Generally, you will see people visiting your website whenever you add something new or to look for a weekly update. Still, it is essential to try to get a system in place that encourages visitors to come to the site regularly.

An interesting idea I have seen on a few websites is creating a login reward. For example, if you land some affiliate deals on your website, create a login requirement which will give your visitors a further discount on the products they seek.

How much time typically passes between your visitor’s logins? Do you have any plans for content that gets visitors to your website every day?

2 thoughts on “How to Show Users Last Login Time in WordPress”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.