Display Friend Count and Recent Connections in the BuddyPress Sidebar

connectionsThis is a quick tutorial that utilizes BuddyPress template tags and a filtered activity loop in order to display how many friends a logged-in user has and recent sitewide connections. Opting to show this information in the sidebar will help you to promote connections across your site and increase users’ interaction. This may not be the best way to do this, but it works. If anyone knows an easier or more appropriate way to call this information to the page, please feel free to post in the comments.

The screenshot shows how these items will appear on the page after following the sequence below. I tested this last night with WordPress Single User 2.9.1. and BuddyPress 1.2 and it works fine, but I’m definitely open to hearing more efficient ways to get this done. This tutorial assumes that you are using the default BuddyPress theme with the “Blog” option selected and you’d like to include some friend-specific information in the sidebar on the blog homepage. Obviously, if you have the Activity layout selected in the theme options, you wouldn’t want to include this as a sidebar.

Step 1. Add the function to show how many friends a user has to the functions.php file of your BuddyPress child theme.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function total_friend_count( $user_id = false ) {
global $wpdb, $bp;

if ( !$user_id )

$user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;

/* This is stored in 'total_friend_count' usermeta.

This function will recalculate, update and return. */

$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) AND is_confirmed = 1", $user_id, $user_id ) );

if ( !$count )

return 0;

update_usermeta( $user_id, 'total_friend_count', $count );

return $count;

}

Step 2. Edit sidebar.php to get the total friend count.

Add this before your dynamic sidebar:

1
2
3
<div id="friendcount">
<h4>My Total Friends (<?php echo total_friend_count( $user_id = false ) ?>)</h4>
</div>

Step 3. Include the activity loop in your sidebar.php, filtered to display friend connections.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div id="friend-connections"><?php if ( bp_has_activities( 'object=friends' ) ) : ?></div>
<div class="pagination">
<div class="pag-count" id="activity-count"><?php bp_activity_pagination_count() ?></div>
<div class="pagination-links" id="activity-pag"><?php bp_activity_pagination_links() ?></div>
</div>
<h4>Recent Connections</h4>
<ul class="activity-list">
<ul class="activity-list"><?php while ( bp_activities() ) : bp_the_activity(); ?>
    <li class="<?php bp_activity_css_class() ?>">
<div class="activity-avatar"><?php bp_activity_avatar() ?></div>
<?php bp_activity_content() ?></li>
</ul>
</ul>
<?php endwhile; ?>

<?php else: ?>
<div class="widget-error"><?php _e('There has been no recent site activity.', 'buddypress') ?></div>
<?php endif;?>

You can find information about how to modify and filter this and other loops to suit your own needs at the BuddyPress codex. There are many optional parameters that you can pass for customization, such as type, the number of users to display before paginating, and the maximum number of users to display. A slew of filters are available for the user ID’s, the various components and their custom actions. Have fun experimenting! Again, if you know of a better way to get the job done, please feel free to post your suggestions for simplification in the comments. If there’s a widget out there for this and I missed it, please let me know. ;)

Comments (6)

  1. Hi I purchased the Out of Box WPMU Install and hosting package for 95.00 USDs and I have yet to see my account instructions. You may have had the wrong email address. Please check on my account and thanks for the tips.

    My Domain is newaecmedia.com and I have it pointing to your nameservers at Pserk.

    It’s been 24 hours. Thanks.

    Curtis

  2. Hello thx for this but it’s a little broken, for example when you go on a member profil page, widget is totally syncro with the friends who are listed on this profile page …. I’ve say this to Damon Cook too.

    If someone know a better way to do this….
    have a nice day

Participate