How to check if a user (not current user) is logged in?

I need to display the online status (online/offline) for each author page (custom author page template).

is_user_logged_in() only applies to the current user and I can’t find a relevant approach targeting the current author e.g. is_author_logged_in()

Any ideas?

One Trick Pony was kind enough to prepare the coding for two to three functions using transients, something I hadn’t use before.

http://codex.wordpress.org/Transients_API

Add this to functions.php:

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

  if(is_user_logged_in()){

    // get the online users list
    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);
    }

  }
}

Add this to author.php (or another page template):

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

Second (do not use)

This answer is included for reference. As pointed out by One Trick Pony, this is undesireable approach because the database is updated on each page load. After further scrutiny the code only seemed to be detecting the current user’s log-in status rather than additionally matching it to the current author.

1) Install this plugin: http://wordpress.org/extend/plugins/who-is-online/

2) Add the following to your page template:

//Set the $curauth variable
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;

// Define the ID of whatever authors page is being viewed.
$authortemplate_id = $curauth->ID;

// Connect to database.
global $wpdb;
// Define table as variable.
$who_is_online_table = $wpdb->prefix . 'who_is_online';
// Query: Count the number of user_id's (plugin) that match the author id (author template page).
$onlinestatus_check = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM ".$who_is_online_table." WHERE user_id = '".$authortemplate_id."';" ) );

// If a match is found...
if ($onlinestatus_check == "1"){
echo "<p>User is <strong>online</strong> now!</p>";
}
else{
echo "<p>User is currently <strong>offline</strong>.</p>";
}

3

I would use transients to do this:

  • create a user-online-update function that you hook on init; it would look something like this:

    // get the user activity the list
    $logged_in_users = get_transient('online_status');
    
    // get current user ID
    $user = wp_get_current_user();
    
    // check if the current user needs to update his online status;
    // he does if he doesn't exist in the list
    $no_need_to_update = isset($logged_in_users[$user->ID])
    
        // and if his "last activity" was less than let's say ...15 minutes ago          
        && $logged_in_users[$user->ID] >  (time() - (15 * 60));
    
    // update the list if needed
    if(!$no_need_to_update){
      $logged_in_users[$user->ID] = time();
      set_transient('online_status', $logged_in_users, $expire_in = (30*60)); // 30 mins 
    }
    

    So this should run on each page load, but the transient will be updated only if required. If you have a large number of users online you might want to increase the “last activity” time frame to reduce db writes, but 15 minutes is more than enough for most sites…

  • now to check if the user is online, simply look inside that transient to see if a certain user is online, just like you did above:

    // get the user activity the list
    $logged_in_users = get_transient('online_status');
    
    // for eg. on author page
    $user_to_check = get_query_var('author'); 
    
    $online = isset($logged_in_users[$user_to_check])
       && ($logged_in_users[$user_to_check] >  (time() - (15 * 60)));
    

The transient expires in 30 minutes if there’s no activity at all. But in case you have users online all the time it won’t expire, so you might want to clean-up that transient periodically by hooking another function on a twice-daily event or something like that. This function would remove old $logged_in_users entries…

Leave a Comment