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
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 );