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

Asked

Viewed 2,330 times

0

I want to check if a specific user is logged in to the site, who knows or works with Wordpress knows this and 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 problem, however as I can show to a visitor who is not logged on the site, that this 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 from the stack, but none worked as most were 2011, 2012 and 2013.

But next that I managed to get a satisfactory result was with the code below, however it is not checking the name of the specified user, it only shows that this online if the person login :(

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

1 answer

1


Iae Vitor! Next man, there is no way to do it by the method you are doing haha! For this is a function... how can I say... that will be called and returned For example, you access the page and the code question pro wordpress "Am I logged in?" and the wordpress just answers yes or no. A user is not able to know if another user is logged in or not, because there is no way to keep an open connection to know this wordpress information

(Actually there is, if he makes a connection via Ajax, but there is only if you want {remembering that if you use ajax in a short period of time it will create simultaneous requests to verify whether the user is online or not, this with the active user sending for example a request to update the database every 30 seconds stating that it is online, if you want to explore this case just say that I try to give an explanation 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 to you how this works is that when you do some activity in wordpress, for example: see, create, edit a post, or whatever, enters some page or post, either go to wordpress settings or any other activity you log in to wordpress it generates a temporary "Log" called "Transient" and it expires in X time (in seconds) then type 3 in 3 minutes you need to do something on the site to be shown as online, If you log on to 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 that to the side where you want to display your author’s name

<?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 helped you :)

  • It worked perfectly, thank you ;)

  • Just for the record, there are other even more practical methods for this, but this is the most basic of all, if you want to explore more, read on functions and on Ajax you will see how you can do many other things from this idea :)

  • I will take a look, until I can also implement the status of absent, thanks to your help I got a basis to put the idea into practice, vlw...

  • Could you show me the missing code? xD I found the idea haha interesting!

  • when I figure out how to do it (if I get né kkk) I’ll put it here on this poster.

  • A solution would be to do two checks, say the function does the first check in 180 seconds and detect that the user is offline/inactive, until then the function would interpret that the user is absent, when the function does a second check and detects that the user is inactive for more than 360 seconds (adding 180 + 180 seconds after the first check) it switches the user offline. As I am layman in PHP I have no idea how to do this, but the idea might work.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.