Home > CMS Tutorial > WordPress > body text

How to display the last login time of logged in users in WordPress frontend

藏色散人
Release: 2019-11-25 13:18:07
forward
2944 people have browsed it

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.

How to display the last login time of logged in users in WordPress frontend

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;
}
Copy after login

Add the calling code at the appropriate location in the theme template:

<?php global $userdata; get_currentuserinfo(); get_last_login($userdata->ID); ?>
Copy after login

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!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!