Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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".

share|improve this question
    
If you try to add code to the template archive nothing happens.This code does not work for archive pages –  Shklyar Sergio Apr 8 '12 at 16:41
2  
What exactly doesn't work? –  kaiser Oct 4 '12 at 9:35
    
What code are you trying to add to the archive template? –  s_ha_dum Nov 3 '12 at 18:06

1 Answer 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; ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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