Show Last login Time Of User In WordPress

How to display Quickly a WordPress user’s last login with custom code .

Actually some time  we need to know our active users of site. so we can differentiate which user hasn’t logged in for a while.

This is combination  4-5 hooks.

1: First we can use wp_login hook for saving login time and date.

<?php
//Record user's last login to custom meta field
add_action( 'wp_login', 'custum_wp_login', 10, 2 );

function custum_wp_login( $user_login, $user ) {
    update_user_meta( $user->ID, 'custum_last_login', date('Y-m-d H:i:s'));
}

 

2: Second we need to add column in user table to show last login

<?php
//Register new custom column with last login time
add_filter( 'manage_users_columns', 'custum_manage_users_columns');
add_filter('manage_users_custom_column',  'custum_manage_users_custom_column', 10, 3);

function custum_manage_users_columns( $columns )
{
    $columns['custum_last_login'] = __('Last login', 'last_login');
    return $columns;
}
function custum_manage_users_custom_column( $value, $column_name, $user_id )
{
    if ( 'custum_last_login' == $column_name){
     $last_login = get_user_meta( $user_id, 'custum_last_login', true );
         $value = date("g:i a - d-M-y", strtotime( $last_login));
    }
    return $value;
}

 

After adding this code you’ll see a new last login column.

 

Categories
Scroll to Top

Request A Quote