Last active
September 17, 2017 14:40
-
-
Save ingozoell/49d424a7ac8c9daded68fd00bf6b74d0 to your computer and use it in GitHub Desktop.
WordPress – How to get last login time in wordpress
https://holisticwp.com/blog/capture-user-last-login-time/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* How to get last login time in wordpress | |
* https://holisticwp.com/blog/capture-user-last-login-time/705 | |
* | |
*/ | |
add_action('wp_login','wpdb_capture_user_last_login', 10, 2); | |
function wpdb_capture_user_last_login($user_login, $user){ | |
update_user_meta($user->ID, 'last_login', current_time('mysql')); | |
} | |
add_action('wp_login','wpdb_capture_user_last_login', 10, 2); | |
function wpdb_capture_user_login($user_login, $user){ | |
$login = get_user_meta($user->ID, 'last_login', true); | |
if(empty($login) || !is_array($login)){ | |
$login = array(current_time('mysql')); | |
} | |
else{ | |
if(count($login) == 10){ | |
array_shift($login); | |
} | |
$login[] = current_time('mysql'); | |
} | |
update_user_meta($user->ID, 'last_login', $login); | |
} | |
add_filter( 'manage_users_columns', 'wpdb_user_last_login_column'); | |
function wpdb_user_last_login_column($columns){ | |
$columns['lastlogin'] = 'Letzter Login'; | |
return $columns; | |
} | |
add_action( 'manage_users_custom_column', 'wpdb_add_user_last_login_column', 10, 3); | |
function wpdb_add_user_last_login_column($value, $column_name, $user_id ) { | |
if ( 'lastlogin' != $column_name ) | |
return $value; | |
return get_user_last_login($user_id,false); | |
} | |
function get_user_last_login($user_id,$echo = true){ | |
$date_format = get_option('date_format') . ' ' . get_option('time_format'); | |
$last_login = get_user_meta($user_id, 'last_login', true); | |
$login_time = 'Bisher kein Login'; | |
if(!empty($last_login)){ | |
if(is_array($last_login)){ | |
$login_time = mysql2date($date_format, array_pop($last_login), false); | |
} | |
else{ | |
$login_time = mysql2date($date_format, $last_login, false); | |
} | |
} | |
if($echo){ | |
echo $login_time; | |
} | |
else{ | |
return $login_time; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment