How to display the status of users (online – offline) in archive.php

I am using the code the user status How to check if a user (not current user) is logged in?. On page profile of the author it works well (thank you guys). But if I put on the archive page, at the bottom of the post, it does not work.

functions.php

Read More
add_action('wp', 'update_online_users_status');
function update_online_users_status(){

    if(is_user_logged_in()){

        if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();

        $current_user = wp_get_current_user();
        $current_user = $current_user->ID;  
        $current_time = current_time('timestamp');

        if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
            $logged_in_users[$current_user] = $current_time;
            set_transient('users_online', $logged_in_users, 30 * 60);
        } 
    }
}

author.php

<?php
function is_user_online($user_id) {

    // get the online users list
    $logged_in_users = get_transient('users_online');

    // online, if (s)he is in the list and last activity was less than 15   minutes ago
    return isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] >     (current_time('timestamp') - (15 * 60)));   
}

$passthis_id = $curauth->ID;
if(is_user_online($passthis_id)){echo 'User is online.';}
else {echo'User is not online.';}
?>

EDIT The OP wants to “display the status at the bottom of each post on the archive page”.

Related posts

Leave a Reply

1 comment

  1. It looks like you’re close on this one.

    Move this function:

    function is_user_online( $user_id ) {
        // get the online users list
        $logged_in_users = get_transient( 'users_online' );
    
        // online, if (s)he is in the list and last activity was less than 15   minutes ago
        return isset( $logged_in_users[$user_id] ) && ( $logged_in_users[$user_id] >     ( current_time( 'timestamp' ) - ( 15 * 60 ) ) );   
    }
    

    to your functions.php file and remove it from any other files. Then in the loop, here’s the code you’ll want to use:

    <?php
    global $post;
    if( is_user_online( $post->post_author ) ) : ?>
        <p>User is online</p>
    <?php else : ?>
        <p>User is offline</p>
    <?php endif; ?>