0

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>';
    }
?>
0

You are going to have to add some Javascript to the page that will check for a timeout.

Remember that PHP is executed on the server, and the servers sends HTML to the visitor's browser. One the page is generated, the server doesn't do anything until the next request comes from that visitor (or anyone else).

If you want something to happen after the page is generated, then you have to do something 'client-side'. Javascript or other code will work on the client side after the server generated (and sends) the page.

1
  • the idea is simple, in this case not the need to be in real time, and only to show to a the visitor, if he refreshes the page and the author of the post is offline, it will appear offline.
    – Vitor Hugo
    Aug 18 '18 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.