Set user status to absent on WordPress

Analyzing the function give to get an idea how to solve the question, I do not know if it would work but what if the function give two checks.

Let’s say the function first checks in the first 3 minutes (180 seconds) and detects that the user is offline / inactive, until then the function would interpret that the user is just away.

When the function performs a second check and it is detected that the user is inactive for more than 6 minutes (360 seconds) and automatically the user would go from the absent to offline status.

I’m not good with PHP, so I have no idea how to do this 🙁

Function (functions.php):

// Updates User Activity Status
add_action(‘init’, ‘riverlab_users_status_init’);
add_action(‘admin_init’, ‘riverlab_users_status_init’);
function riverlab_users_status_init(){
$logged_in_users = get_transient(‘users_status’); // Capture users’ activities by wordpress transients
$user = wp_get_current_user(); // Capture of current user data
// Update the user if it is not in the list, or if it is not online for the last 3 minutes (180 seconds)
if ( !isset($logged_in_users[$user->ID][‘last’]) || $logged_in_users[$user->ID][‘last’] <= time()-180 ){
$logged_in_users[$user->ID] = array(
‘id’ => $user->ID,
‘username’ => $user->user_login,
‘last’ => time(),
);
set_transient(‘users_status’, $logged_in_users, 180); // Set to expiry every 3 minutes (180 seconds)
}
}
// Check if someone is online in the last 3 minutes
function riverlab_is_user_online($id){
$logged_in_users = get_transient(‘users_status’);

return isset($logged_in_users[$id][‘last’]) && $logged_in_users[$id][‘last’] > time()-180;
}
// Check the last time someone was online
function riverlab_user_last_online($id){
$logged_in_users = get_transient(‘users_status’);
if ( isset($logged_in_users[$id][‘last’]) ){
return $logged_in_users[$id][‘last’];
} else {
return false;
}
}

Add in single.php:

<?php $id = get_the_author_meta( ‘ID’ );
// this should be inside a post (single.php for example)
if ( riverlab_is_user_online($id) ) {
echo ‘<span class=”tag-status tag-status-online”></span>’;
} else {
echo ‘<span class=”tag-status tag-status-offline”></span>’;
}
?>

Read more here:: Set user status to absent on WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *