Checking if user is logged in to Wordpress (show for visitor)

0

I want to check if a specific user is logged in to the site, who knows or works with Wordpress knows that this is a very basic thing to do with the code below:

<?php if (is_user_logged_in()) {
    echo 'Olá, Usuário logado';
    } else {
    echo 'Bem-vindo, visitante!';
}
?>

So far no problems, however, as I can show a visitor that is not logged in to the site, that specific user is online.

Example: Let's say I'm the author of a post, and I want it to appear to the visitor in my bio that I'm online on the site, how can I do that?

I tried to use several other methods available in other questions here in the stack, but none worked because most were from 2011, 2012 and 2013.

The next thing I got to get a satisfactory result was with the code below, however it is not verifying the specified user name, it only shows that it is online if the person logs in:

<?php if ( is_user_logged_in() ) {
    $user = wp_get_current_user();
    $user && isset($user->user_login) && 'NOME_DO_USUARIO' == $user->user_login);
        echo '<span class="tag-status tag-status-online"></span>';
    }
    else {
        echo '<span class="tag-status tag-status-offline"></span>';
    }
?>
    
asked by anonymous 17.08.2018 / 02:20

1 answer

1

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:)

    
17.08.2018 / 04:31