Iae Vitor! Next face, you can not do it by the method you're doing haha!
For this is a function ... how can I say ... that it will be called and returned
For example, you access the page and the code asks pro wordpress "Am I logged in?" and wordpress just answers yes or no.
A user is not able to know if another user is logged in or not, since he can not keep an open connection to know that wordpress information
(Actually it does, if it makes a connection via Ajax, but that's only if you want to (remembering that if you use ajax in a short period of time it will create simultaneous requests to check if the user is online or no, this with the active user sending for example an update request to the database every 30 seconds informing you that you are online, if you want to explore this case, just say that I try to give an esplanade about})
But I currently use this on my website and this is code:
<?php
// Atualiza o Status de Atividade do usuário
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'); // Captura as atividades dos usuários pelos transients do wordpress
$user = wp_get_current_user(); // Captura dos dados do usuário atual
// Atualiza o usuário se ele não estiver na lista, ou se ele não estiver online durantes os ultimos 3 minutos (180 segundos)
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); // Setar para que expire de 3 em 3 minutos (180 segundos)
}
}
// Checar se há alguém online nos ultimos 3 minutos
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;
}
// Checa a ultima vez que alguém esteve 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;
}
}
?>
Trying to explain how this works is that when you do some activity in wordpress, such as: see, create, edit a post, or whatever, go to some page or post, or go to wordpress settings or any another activity that logs into wordpress it generates a temporary "Log" called "Transient" and it expires in X time (in seconds) then type in 3 minutes you need to do something on the site so it can be shown as online if you enter the site and leave quickly, your online status will still remain for 3 minutes.
You can put this code at the end of your functions.php
and to display the information on your site you can add this to the side where you want to display the name of your author
<?php
$id = get_the_author_meta( 'ID' ); // isso deve estar dentro de um post (single.php por exemplo)
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>';
}
?>
I hope I have helped you:)