WordPress User Last Login

Have you ever needed to know a WordPress user last login date?  By default, WordPress doesn’t provide any information on when a user last logged in.  However, such information is extremely useful when, for example, trying to delete inactive users or evaluate the success of a membership site.

The User Meta API

Thankfully, WordPress allows you to store additional information on users.  This additional information is known as user meta and, as of WordPress 3.0, can be managed using a few basic functions:

Saving the WordPress User Last Login

Let’s take a quick look at the code that will actually save the timestamp of a user’s last login:


View the code on Gist.

We use the wp_login action to get the user object when a user logs on.  All we do then is use the time() function to fetch the current timestamp and save it as user meta.  To fetch the last login for a user later, this is all we have to do:

get_user_meta( $user->ID, '_last_login', true );

Submit a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>