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.


Add this to functions.php:

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


    // 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;
echo 'User is online.';}
else {
echo'User is not online.';}

Second Answer (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));

// 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>";
echo "<p>User is currently <strong>offline</strong>.</p>";
  • That is one of the worst solutions you could find. That plugin is updating the db on each page load... – onetrickpony Nov 22 '11 at 1:42
  • Thanks for reviewing it. I'll steer clear. I've replied to your answer (below). I'm stuck on creating the function. – Dominor Novus Nov 22 '11 at 2:11

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

  • Transients are new to me. Thanks for the code. I can't implement the first step. I copied your first code block and pasted it into function user_online_update() { [YOUR CODE] }add_action('init', 'user_online_update'); in my functions.php file - I'm receiving a server error which suggests that something isn't parsing right or I've misinterpreted your instructions. – Dominor Novus Nov 22 '11 at 2:01
  • 1
    +1 for suggesting to use transients. Not many people know about them. – Dwayne Charrington Nov 22 '11 at 2:52
  • @Dominor: the complete code. – onetrickpony Nov 22 '11 at 11:28
  • @OneTrickPony: I added the action to functions.php. Where do I add the second function (see line 27 of your complete code)? I tried authors.php and then functions.php but to no avail. I tried using the condition on line 25 of your complete code. Each time, the page returns a server error or else doesn't parse anything in my page beyond your code. I've removed my original code and deactivated the "Who is online?" plugin. What am I overlooking? – Dominor Novus Nov 22 '11 at 15:36
  • replace $time_limit with 15 and make sure you correctly opened/closed php tags... – onetrickpony Nov 23 '11 at 12:09

To my knowledge there is not a way to do this using the built-in WordPress functions, but don't let that discourage you; write a plugin!

One way you could do this is by creating a new table in the database that simply tracks the last time the user was active on the site. You could also have a settings page for your plugin that determined how long you would consider a registered user to be "Logged in".

You would implement this using a WordPress hook. I'd start by hooking into the login, so that once a user logs in, your plugin logs the time in the database. You could then explore other things like setting their status to 'away' if they click logout, or 'idle' if their login time was more than two hours ago.

You would run into an issue if they are logged in and active on the site, but past this two hour window. In this case, you'd need to be hooked into the wp-admin section so that anytime they do anything in wp-admin it updates your database to the current time.

Then, on the posts, you would need to do two things: get the author of the current post:

<?php $user_login = the_author_meta( $user_login ); ?>

then query your database to determine if they are logged in:

<?php if your_plugin_function($user_login)... ?>
...display something...
  • Based on your solution I've found a plugin that creates the table and allows the lapsed time to be define via settings. After reviewing the plugin files, I'm not sure how to manipulate the code to output online/offline in my author template page. If you could glance at the plugin files and point me in the right direction, I'd appreciate it: wordpress.org/extend/plugins/who-is-online (Using your instructions, I'll go as far as I can go in the meantime using a hook in my functions.php file) – Dominor Novus Nov 21 '11 at 23:58
  • Success! I queried the plugin database for user ID and author ID matches. I've pasted the code in my question (above). – Dominor Novus Nov 22 '11 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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