如何在WordPress中显示用户的上次登录日期

Recently, one of our readers asked us how to show a user’s last login date in WordPress. You may need this if you wanted to add an author activity box on your WordPress site. In this article, we will show you how to display user’s last login date in WordPress.

最近,一位读者问我们如何在WordPress中显示用户的最后登录日期。 如果您想在WordPress网站上添加作者活动框,则可能需要此功能。 在本文中,我们将向您展示如何在WordPress中显示用户的上次登录日期。

Showing a user's last login date in WordPress
影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

方法1:在WordPress管理区域中显示用户的上次登录日期 (Method 1: Showing a User’s Last Login Date in WordPress Admin Area)

This method is easier, but it will only show a user’s last login date inside WordPress admin area.

这种方法比较简单,但是只会在WordPress管理区域内显示用户的最后登录日期。

First thing you need to do is install and activate the WP Last Login plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活WP Last Login插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit ‘Users’ page in the admin area. You will notice a new column showing each user’s last login date.

激活后,您需要访问管理区域中的“用户”页面。 您会注意到一个新列,显示每个用户的上次登录日期。

Last login date column in WordPress admin area

At first it may show ‘never’ for all users. That’s because a user needs to login since the plugin was activated so that it could capture last login date and store it.

起初,它可能对所有用户都显示“从不”。 这是因为自激活插件以来,用户需要登录,以便它可以捕获上次登录日期并进行存储。

方法2:在WordPress中手动显示用户的上次登录日期 (Method 2: Manually Show User’s Last Login Date in WordPress)

This method allows you to display a user’s last login date anywhere on your WordPress site.

此方法使您可以在WordPress网站上的任何位置显示用户的上次登录日期。

Simply add this code to your theme’s functions.php file or a site-specific plugin.

只需将此代码添加到主题的functions.php文件或特定站点的插件中即可

If you’re new to adding code, then please read this guide on pasting code from the web.

如果您不熟悉添加代码,请阅读本指南,了解如何从网络粘贴代码


<?php 
/**
 * Capture user login and add it as timestamp in user meta data
 *
 */

function user_last_login( $user_login, $user ) {
    update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );

/**
 * Display last login time
 *
 */
 
function wpb_lastlogin() { 
	$last_login = get_the_author_meta('last_login');
	$the_login_date = human_time_diff($last_login);
	return $the_login_date; 
} 

/**
 * Add Shortcode lastlogin 
 *
 */
 
add_shortcode('lastlogin','wpb_lastlogin');
?>

This code adds last login as a meta key. Each time a user logs in, it saves the time as a meta key value. Before you want to test the plugin, you need to logout of WordPress and then login again.

此代码将最后一次登录添加为元密钥。 每次用户登录时,都会将时间保存为元键值。 在测试插件之前,您需要注销WordPress,然后再次登录。

You can then display this meta key value using [lastlogin] shortcode in your WordPress posts and widgets.

然后,您可以在WordPress帖子和小部件中使用[lastlogin] 简码显示此元关键字值。

If you want to show last login information in your child theme, then you can add this code:

如果要在子主题中显示最后的登录信息,则可以添加以下代码:


<?php echo 'Last seen: '. do_shortcode('[lastlogin]') .' ago'; ?>

Showing user's last login information in author bio box

As you would notice that this code displays relative date and time, i.e. ‘2 hours ago’ instead of full date and time. If you want to display the full date and time, then locate this line in the code above:

如您所见,此代码显示相对日期和时间 ,即“ 2小时前”,而不是完整的日期和时间。 如果要显示完整的日期和时间,请在上面的代码中找到以下行:

$the_login_date = human_time_diff($last_login);

Now replace it with this line:

现在,将其替换为以下行:

$the_login_date = date('M j, Y h:i a', $last_login);

The ‘M j, Y h:i a’ part in this code is called date and time format string. If you want to change how this code displays date and time, then check out our guide on how to change date and time format in WordPress.

此代码中的“ M j,Y h:i a”部分称为日期和时间格式字符串。 如果您想更改此代码显示日期和时间的方式,请查看有关如何在WordPress中更改日期和时间格式的指南。

We hope this article helped you learn how to show user’s last login date in WordPress. You may also want to see our guide on how to add author info box in WordPress posts.

我们希望本文能帮助您学习如何在WordPress中显示用户的上次登录日期。 您可能还想查看有关如何在WordPress帖子中添加作者信息框的指南。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/plugins/how-to-show-users-last-login-date-in-wordpress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值