Skip to Main Content

How to display the date of last connection to a user on WordPress

Divi: the easiest WordPress theme to use

Divi: The best WordPress theme of all time!

With over 901.000 downloads, Divi is the most popular WordPress theme in the world. It is complete, easy to use and comes with more than 62 free templates. [Recommended]

Would you like to display the last login date of a user of your WordPress blog? You may need this functionality especially if you want to display an activity section in the user profile. In this tutorial, I'll show you how to display a user's last logged in 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

Easily create your website with Elementor

Elementor allows you to easily create any website design with a professional look. Stop paying expensive for what you can do yourself. [Free]

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.

All you have to do is add the following code in your active theme or in 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] 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 it for this tutorial. I hope it will allow you to display the date of last login of the users of your WordPress blog.

This article features 0 comments

Leave a comment

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

This site uses Akismet to reduce unwanted. Learn more about how your comments data is used.

Back To Top