Adding a Friends Online widget to your BuddyPress community

The goal of this trick is to display the friends of a displayed user once in his member area. Just like on BuddyPress.org or wpmu.org. I only added an ajax call to display those who are online.. I haven’t checked wpmu.org’s plugin, but this widget mimics the functionality on buddypress.org, and it does not displays the friends of the logged in user but the friends of the user currently being viewed.

Step 1: Edit Sidebar.php

Add the template sidebar.php to your Child Theme and open it. just before calling the dynamic sidebar (around line 68) add this code :


                
        

Step 2: Edit Functions.php

In the functions.php file of the Child theme add the following functions :

function imath_bp_display_friends(){
        global $bp;
        $user_id = $bp->displayed_user->id;
        $user_all_friends_url = $bp->displayed_user->domain . BP_FRIENDS_SLUG;
        ?>
        

- View All - View Online

No friends !


displayed_user->id; if(isset($_POST['offoronline'])) $type = $_POST['offoronline']; else $type = 'offline'; if($type=='offline') $args = 'user_id='.$user_id.'&per_page=10&max=10&populate_extras=0'; else $args = 'user_id='.$user_id.'&type=online&per_page=10&max=10&populate_extras=0'; if ( bp_has_members( $args ) ){ ?>
No friends online!'; else echo '

No friends!

'; } die(); } function imath_bp_display_friends_js(){ if ( bp_is_member() ){ ?>

Voilà! Sorry for my english

BuddyPress Hosting Guide Looking for quality BuddyPress hosting? We will guide you through all the available hosting options and help you find the best webhost for your next BuddyPress project!

14 Responses to Adding a Friends Online widget to your BuddyPress community

  1. Slava UA December 9, 2010 at 6:39 pm #

    Hey, cool!
    I created a widget for BP 1.1 that did absolutely the same but in a different way (at that time there were not so many handy bp_ functions).

    Reply
  2. imath December 9, 2010 at 9:24 pm #

    hi Slava UA,
    thks 4 your comment.

    Reply
  3. Anindya Ray December 10, 2010 at 10:45 am #

    How to add a “Follow this member!” button on the activity stream, like in this site ?

    note: I didn’t get an appropriate place to request for this , so posted here, kindly forgive any mistake …

    Reply
  4. imath December 10, 2010 at 4:06 pm #

    you should directly ask @bowe ;)

    Reply
  5. momo December 10, 2010 at 4:51 pm #

    Merci imath

    Reply
    • imath December 10, 2010 at 10:35 pm #

      de rien ;)

      Reply
  6. imath December 11, 2010 at 12:38 pm #

    just 4 info, thanks to the wpmu.org suggestion, it’s now a plugin called BP Show Friends ;)

    Reply
    • bowe December 17, 2010 at 2:18 pm #

      Great! Well done :)

      Reply
  7. Lewis December 21, 2010 at 2:47 pm #

    Hey, I can’t get this to work, and the plugin does not do what you say this tutorial does, it shows all your friends, I only want to show the ones online but there are no widgets for that and this did not work, is there a specific place I put the code in the functions.php file?

    Reply
    • imath December 24, 2010 at 8:14 am #

      Hi Lewis,

      I advise you to use the widget BP Show Friends. Once installed, go to WordPress backend, then drag and drop the widget in your sidebar.
      Once on your member’s page (siteurl/members/yourname) it will show your friends. Under the title of the widget, there are 2 links calling ajax functions, the second link will display the friends that are online or a message ‘No friends online’..
      So the plugin actually do what this tutorial does !!

      Reply
  8. Jeff Gaudette January 13, 2011 at 9:24 pm #

    Cool plugin, Imath. Thank you for taking the time to develop this.

    One question. If I wanted to change the title of the widget to say “current members” friends, where “current member” is the name of the users who’s page you are on, how would I do that? I see the h3 title, but I am not sure how to echo the members name.

    Thanks very much

    Reply
  9. Himaltiertams September 19, 2011 at 4:23 pm #

    Hi!
    Very interesting name by the forum bp-tricks.com

    It is the truth.

    Reply

Trackbacks/Pingbacks

  1. cé ki tes amis ? » Article » imath.. - December 9, 2010

    [...] j’ai rapidement écrit 2 fonctions pour soumettre ma première contribution. Elle est disponible ici mais comme j’ai tenté de la rédiger en anglais (en fait on se rapproche plus du franglish!) [...]

  2. 15 Go-To Places for WordPress and BuddyPress News, Tips and Tutorials | WordPress, Multisite and BuddyPress plugins, themes, news and help – WPMU.org - February 23, 2011

    [...] Recent Highlight: Adding a Friends Online Widget to Your BuddyPress Community [...]

Leave a Reply