Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio

27

Jun

How to get a user last login time in wordpress

How to get a user last login time in wordpress

Posted On : June 27, 2013

| No Comment

Sometimes in our ongoing WordPress projects, Client wants to show user last login time. So we found function get_user_meta() to get it. How to get user last login time in WordPress ? Past this functions in theme functions.php file.

Code1:

//function for setting the last login
function set_last_login($login) {

$user = get_userdatabylogin($login);

//add or update the last login value for logged in user
update_usermeta( $user->ID, ‘last_login’, current_time(‘mysql’) );
}

//function for getting the last login
function get_last_login($user_id) {
global $last_login;

$last_login = get_user_meta($user_id, ‘last_login’, true);

//picking up wordpress date time format

$date_format = get_option(‘date_format’) . ‘ ‘ . get_option(‘time_format’);

//converting the login time to wordpress format

$the_last_login = mysql2date($date_format, $last_login, false);

//echo $the_last_login;
//finally return the value
//return $the_last_login;
}

//get current user object
$current_user = wp_get_current_user();

//get the last login time by passing the user id to the above function.
echo get_last_login($current_user->ID);Then to call this from any of your theme pages, just paste in this code:

Code2:

This makes it easy to pull the last login date and also it also uses the WordPress date/time formats you set on your blog settings page. Hope that helps you all.

Screenshot

Comment