简体   繁体   English

如何通过用户ID检查WordPress是否在线?

[英]How can I check if a user is online in WordPress by his id?

I want to show an online status on my website if an other user is online. 如果其他用户在线,我想在我的网站上显示在线状态。 So for example if user A wants to know if user B is available, I want to show an online sign. 因此,例如,如果用户A想知道用户B是否可用,我想显示一个在线标志。

I know that there is a function in WordPress called is_user_logged_in() but this function works only for the current user. 我知道WordPress中有一个名为is_user_logged_in()的函数,但该函数仅适用于当前用户。 https://developer.wordpress.org/reference/functions/is_user_logged_in/ So is there anyone who has an idea how I can get this done? https://developer.wordpress.org/reference/functions/is_user_logged_in/有没有人知道如何完成此工作?

This is the logic: 这是逻辑:

if ( user_online( $user_id ) ) {
    return 'Online';
} else {
    return 'Absent';
}

You might use Transients API to get users's status. 您可以使用Transients API来获取用户的状态。 Create a user-online-update function that you hook on init . 创建一个您挂在init上的user-online-update函数。 For example: 例如:

// get logged-in users
$logged_in_users = get_transient('online_status');

// get current user ID
$user = wp_get_current_user();

// check if the current user needs to update his online status;
// status no need to update if user exist in the list
// and if his "last activity" was less than let's say ...15 minutes ago  
$no_need_to_update = isset($logged_in_users[$user->ID]) 
    && $logged_in_users[$user->ID] >  (time() - (15 * 60));

// update the list if needed
if (!$no_need_to_update) {
  $logged_in_users[$user->ID] = time();
  set_transient('online_status', $logged_in_users, $expire_in = (30*60)); // 30 mins 
}

This should run on each page load, but the transient will be updated only if required. 这应该在每次页面加载时运行,但是仅在需要时才更新瞬态。 If you have a large number of users online you might want to increase the "last activity" time frame to reduce db writes, but 15 minutes is more than enough for most sites. 如果您有大量在线用户,则可能需要增加“上次活动”时间范围以减少数据库写入,但是对于大多数站点而言,15分钟就足够了。

Now to check if the user is online, simply look inside that transient to see if a certain user is online, just like you did above: 现在要检查用户是否在线,只需在瞬态内部查看,看看某个用户是否在线,就像上面的操作一样:

// get logged in users
$logged_in_users = get_transient('online_status');

// for eg. on author page
$user_to_check = get_query_var('author'); 

$online = isset($logged_in_users[$user_to_check])
   && ($logged_in_users[$user_to_check] >  (time() - (15 * 60)));

The transient expires in 30 minutes if there's no activity at all. 如果根本没有任何活动,则瞬变将在30分钟后过期。 But in case you have users online all the time it won't expire, so you might want to clean-up that transient periodically by hooking another function on a twice-daily event or something like that. 但是如果万一用户一直在线,它不会过期,那么您可能希望通过在每天两次的事件或类似事件上挂接另一个功能来定期清理此瞬态。 This function would remove old $logged_in_users entries... 此功能将删除旧的$logged_in_users条目...

Source: https://wordpress.stackexchange.com/a/34434 资料来源: https : //wordpress.stackexchange.com/a/34434

    First get the user id of the user B by 

    $user_id_B = get_current_user_id();

    Now here give the condition for the particular user B to check whether he is online or not

if(is_user_logged_in()){
    if( $user_id_B == 'user id of B')
    {
        return 'Online'; (or echo 'online';)
    }
}
By this you will get the presence of user B.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM