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:

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:

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

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.

7 thoughts on “Buddypress – Check if a user is online

    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.

  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?

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 class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">