How To Display User’s Last Login Date In WordPress

Are you facing problem to show last login date of user in WordPress? Don’t Worry! Keep reading this guide, here we will explain to you.

If you want to add an author activity box to your WordPress site, you may need it.

However, in this post, we will describe you the same how can show last login date of user.

Now let’s take a look.

We will show you two methods to display login date:

Method 1: Showing a User’s Last Login Date in WordPress Admin Area

This method is very easy to use, but it will only show the user’s last login date within the WordPress admin area.

The very first thing, you have to install the WP Last Login plugin and then activate it. If you are beginners then read our guide on how to install a plugin in WordPress.

After activation, you have to go to the ‘Users’ page in the Admin Area. You will see a new column showing each user’s last login date.

Firstly it can show ‘never’ for all users. This is because since the plugin has been activated, the user needs to login. 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 show the last login date of user anywhere on your site.

Just add this code to your theme’s functions.php file or site-specific plugin.

<?php

/**

* Capture the user login and then 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’);

?>

However this code adds the last login as a meta key. Every time the user logs in, it saves time as the meta key value.

Before testing the plugin, you have to log out of WordPress and then have to login again.

You can then show this meta key value by using the [lastlogin] shortcode in your WordPress posts and widgets.

you can add below given code, if you want to show last login information in your child theme.

 

<?php echo ‘Last seen: ‘. do_shortcode(‘[lastlogin]’) .’ ago’; ?>

As you will see, this code shows the relative date and time, i.e. ‘2 hours ago’ instead of the full date and time.

If you want to show the full date and time, then locate 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 ‘Mj, Yh: i a’ part called date date and time format string in this code. In case you want to change how this code shows the date and time, see our guide on how to change the date and time format in WordPress.

CONCLUSION

Here, we have discussed How To Display User’s Last Login Date In WordPress.

Often, we concluded these methods step by step. Thus, we hope this blog will help you to display last login date.

However, If you have any problem with the same, then, let me know in the comments section. Infect, If you liked this post, please share it with your peers.

Leave a Reply

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