Buddypress – Check if a user is online

Buddypress function/code to check if a user is online.
You can use this function to check if a given buddypress user is online.

The function:

Add following code into your functions.php file:

<?php 
/**
* http://webdeveloperswall.com/buddypress/check-if-a-user-is-online
**/
function is_user_online($user_id, $time=5){
	global $wp, $wpdb;
	
	$user_login = $wpdb->get_var( $wpdb->prepare( "
		SELECT u.user_login FROM $wpdb->users u JOIN $wpdb->usermeta um ON um.user_id = u.ID 
		WHERE 	u.ID = $user_id 
		AND um.meta_key = 'last_activity'
		AND DATE_ADD( um.meta_value, INTERVAL $time MINUTE ) >= UTC_TIMESTAMP()
		" 	
	));
	if(isset($user_login) && $user_login !=""){
		return true;
	}
	else {return false;}
}
?>

Usage :

The function takes a user id as input and return a boolean value of true/false indicating user is online/offline respectively.
A simple use:

<?php
if(is_user_online(3)){
	echo "user id 3 is online";
}
else{
	echo "user id 3 is offline";
}
?>

Basically, by default it checks if the said user is active in last 5 minutes or not.
The function takes second optional argument time.

<?php
if(is_user_online(3, 10)){
	echo "user id 3 is active in last 10 minutes";
}
else{
	echo "user id 3 is inactive since last 10 minutes";
}
?>

The above code checks if the user is active since last 10 minutes or not.

That’s it in here. Hope you find it helpful.

8 thoughts on “Buddypress – Check if a user is online”

  1. Hi, this i s a great snippets, but is there a way to check for all users instead of just a user.

    this could be a good step to display in member header or member directory if user is online.

    I really like your bp message attachment, great plugin.

    Thanks

    1. Thnx abbey, nice to hear that you liked the plugin.

      is there a way to check for all users instead of just a user. this could be a good step to display in member header or member directory if user is online

      Well, you can check if the displayed user is online and show respective message, or online/offline image in member header. Something like:

      <?php
      global $bp;
      if(is_user_online($bp->displayed_user->id)){
      echo “<img src='”.bloginfo(‘stylesheet_directory’).”/images/online.png’ />”;
      }
      else{
      echo “<img src='”.bloginfo(‘stylesheet_directory’).”/images/offline.png’ />”;
      }
      ?>

      makes sense?

    1. $wp and $wpdb are global variables available in wordpress. $wp provides abstraction to interact with wordpress core. $wpdb provides abstraction, security etc to communicate with wordpress database.

  2. hi , when i use this code give an error :
    Warning: Missing argument 2 for wpdb::prepare(), called in D:\xampp\htdocs\hamava\wp-content\themes\huddle\functions.php on line 550 and defined in D:\xampp\htdocs\hamava\wp-includes\wp-db.php on line 1147
    can help me ?
    my buddypress version : Latest 2.0.2

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>