Home > Article > CMS Tutorial > How to display the last login time of logged in users in WordPress frontend
WordPress user functions are relatively weak. Some user information that many Chinese people are familiar with does not have default calling codes, such as user registration time, last login time, etc. The following is the WordPress Tutorial column Let me share with you a code that displays the user’s last login time on the front desk.
You can add the following code to the current theme functions.php:
// 记录登录时间 function user_last_login($user_login) { global $user_ID; // 纠正8小时时差 date_default_timezone_set(PRC); $user = get_user_by( 'login', $user_login ); update_user_meta($user->ID, 'last_login', date('Y-m-d H:i:s')); } add_action('wp_login','user_last_login'); // 调用最后登录时间 function get_last_login($user_id) { $last_login = get_user_meta($user_id, 'last_login', true); $date_format = get_option('date_format') . ' ' . get_option('time_format'); $the_last_login = mysql2date($date_format, $last_login, false); echo $the_last_login; }
Add the calling code at the appropriate location in the theme template:
<?php global $userdata; get_currentuserinfo(); get_last_login($userdata->ID); ?>
If you want to display the last login time in the background user list, you can install the plug-in: WP Last Login.
The above is the detailed content of How to display the last login time of logged in users in WordPress frontend. For more information, please follow other related articles on the PHP Chinese website!