AppThemes Docs

Save the Users Last Login Date and Time

If you’ve ever wanted to capture and display your WordPress users last login date and time, this tutorial is for you. It’s a great way to see how active your current user base is, if certain customers ever logged in, and for security audit purposes.

This tutorial assumes you know how to edit your theme’s functions.php file.

Step 1

First, we’ll want to insert the date and time whenever a user logs into your site. This function uses the wp_login hook and will store the data in a user meta field.

// insert the last login date for each user
function insert_last_login( $login ) {
    global $user_id;
    $user = get_userdatabylogin( $login );
    update_user_meta( $user->ID, 'last_login', gmdate( 'Y-m-d H:i:s' ) );
add_action( 'wp_login', 'insert_last_login' );

Step 2

Next, we need to add a new user column called, “Last Login”. This will be visible on your “Users” => “All Users” WordPress admin screen. Below the code you inserted above, paste in the following:

// add a new "Last Login" user column
function add_last_login_column( $columns ) {
    $columns['last_login'] = __( 'Last Login', 'last_login' );
    return $columns;
add_filter( 'manage_users_columns', 'add_last_login_column' );

Step 3

The last bit of code we need to add will populate the actual user login data in the column we created above. Below the previous code you pasted in your functions.php file, add this:

// add the "Last Login" user data to the new column
function add_last_login_column_value( $value, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    if ( 'last_login' == $column_name && $user->last_login )
        $value = date( 'm/d/Y g:ia', strtotime( $user->last_login ) );
    return $value;
add_action( 'manage_users_custom_column', 'add_last_login_column_value', 10, 3 );

Now visit your WordPress admin users page and you should see the new column. If you already logged in, it should show your last login date/time.

Congrats, you’ve completed this tutorial!

Like this tutorial? Subscribe and get the latest tutorials delivered straight to your inbox or feed reader.

Your rating: none
Rating: 5 - 2 votes