Would you like to display the last login date of a user of your WordPress blog ? You may need this feature especially if you want to display an activity section in the users profile. In this tutorial, I will show you how to display a user's last login date in WordPress.

date of last WordPress login

Method 1: Show login date in WordPress dashboard

This method is fairly simple, but will only display the date a user last logged on the WordPress dashboard.

The first thing you need to do is install and activate the plugin " WP Last Login ". For more details, you can read our tutorial on how to install a WordPress Plugin.

After activating the plugin, all you have to do is access the list of users. From this location, you will notice that a new column has been added to the user table.

List of users last login

For the first time, you will notice that for all users in the column " last connection ", it is written " Never ". The reason is that the plugin has not started recording everyone's connections yet. So, as soon as you log in again, you will notice that the field in this column will change.

Method 2: How to display a user's last logged in date manually

As you might expect, this method is for those who don't have a problem with the codes. Remember we showed you how to create a WordPress plugin.

So all you have to do is add the following code to your active theme or your WordPress Plugin.

ID, 'last_login', time ()); } add_action ('wp_login', 'user_last_login', 10, 2); / ** * Display last login time * * / function bpc_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', 'bpc_lastlogin'); ?>

This code will add a new meta key. Each time a user logs in, he will save the time in the new meta key.

Before you start using the plugin, you will need to log out and log in again. You can display the last connection date using the following shortcode " [Lastlogin] » either on your WordPress blog.

You can also display this information in your child theme using the following code:


demo presentation last visit

As you can see, the code displays a relative date (2 hours ago or "2 hours ago"), instead of the full date. If you want to display the full date, locate the following portion of code in the previous code:

$ The_login_date = human_time_diff ($ last_login);

Now replace that line with the following:

$ The_login_date = date ('M j, Y h: i a', $ last_login);

The format " M j, Y h: ia Is a date format. You can manipulate it to change the position of each element of the date (hour, year, day, etc).

That's all for this tutorial. I hope it will allow you to display the last login date of your users. WordPress blog.