If you want to display the last login date and time of the registered user in your WordPress site, then keep your close attention in this tutorial as I am going to share how to capture the last login date and time of user using simple code snippet.
If you have not the coding knowledge, then you can use the User Last Login (https://wordpress.org/plugins/user-last-login) WordPress plugins as well.
Copy the below WordPress code snippets and use it at the end of the wp-content/your-theme-folder/functions.php file and save it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php /* * how to Capture users last login date and time */ function user_set_last_login($login, $user) { $user = get_user_by('login',$login); $time = current_time( 'timestamp' ); $last_login = get_user_meta( $user->ID, '_last_login', 'true' ); if(!$last_login) { update_usermeta( $user->ID, '_last_login', $time ); }else { update_usermeta( $user->ID, '_last_login_prev', $last_login ); update_usermeta( $user->ID, '_last_login', $time ); } } function wpsnipp_get_last_login($user_id,$prev=null) { $last_login = get_user_meta($user_id); $time = current_time( 'timestamp' ); if(isset($last_login['_last_login_prev'][0]) && $prev) { $last_login = get_user_meta($user_id, '_last_login_prev', 'true' ); } else if(isset($last_login['_last_login'][0])) { $last_login = get_user_meta($user_id, '_last_login', 'true' ); } else { update_usermeta( $user_id, '_last_login', $time ); $last_login = $last_login['_last_login'][0]; } return $last_login; } add_action('wp_login','user_set_last_login', 0, 2); ?> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.