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?
Answer
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 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));
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>";
}
I would use transients to do this:
create a user-online-update function that you hook on
init
; it would look something like this: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:
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…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:
then query your database to determine if they are logged in:
I decided to revisit the given answer and adjust it to the fit the comments.
One thing that was talked about in the comment was some kind of daily transient cleaner. As pointed out by @onetrickpony we can use
wp_schedule_event()
to setup a daily reset.I decided to implement an easy way to fetch all users currently online and recently offline.
The whole thing is object oriented based, packaged into a class.
Packaging it all in a nice plugin.
I’ve also made a plugin for an easy implementation. It’s open source on GitHub @ https://github.com/amarinediary/WPC-User-Activity-Monitoring.
There is additional documentations and functionalities.