Display Friend Count and Recent Connections in the BuddyPress Sidebar

January 29, 2010  | 
6 Comments

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.

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:

<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.

<div id="friend-connections">
<?php if ( bp_has_activities( 'object=friends' ) ) : ?>
	<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"">
	<?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>
	<?php endwhile; ?>
	</ul>
<?php else: ?>
	<div class="widget-error">
		<?php _e('There has been no recent site activity.', 'buddypress') ?>
	</div>
<?php endif;?>
</div>

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. ;)

Featured Plugin - Add bottom corner (or anywhere else) chat to your site

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Featured Plugin - Start Your Own Powerful Membership Site

If you're thinking about starting a paid, or just private, membership site then this is truly the plugin you've been looking for. Easy to use, massively configurable and ready to go out of the box!
Find out more

Featured Plugin - Every great SEO tweak you need, in one snazzy bundle

Fully integrated with the SEOMoz API, complete with automatic links, sitemaps and SEO optimization of your WordPress setup - this is the only plugin you need to help you rank your site number 1 on Google - nothing else compares.
Find out more

Featured Plugin - Send beautiful html email newsletters, from WordPress!

Now there's no need to pay for a third party service to sign up, manage and send beautiful email newsletters to your subscriber base - this plugin has got the lot.
Find out more

Featured Plugin - Open an Online Store with MarketPress

Out of all the WordPress ecommerce plugins available, this has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

Featured Plugin - Easily integrate your WordPress site with Facebook

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Featured Plugin - WordPress + Google Maps = Perfect

Simply insert google maps into posts, sidebars and pages - show directions, streetview, provide image overlays and do it all from a simple button and comprehensive widget.
Find out more

Featured Plugin - Turn any WordPress page into a fully featured wiki!

To get a wiki up and running you used to need to install Mediawiki and toil away for days configuring it... not any more! This plugin gives you *all* the functionality you want from a wiki, in WordPress!!!
Find out more

Featured Plugin - Host sites, get paid, just like WordPress.com

If you've ever wondered how you could offer a paid site management and hosting service, then this is the plugin for you. Offer a freemium or paid service, for any niche you like, it's powered Edublogs.org to success already!
Find out more

6 Responses to Display Friend Count and Recent Connections in the BuddyPress Sidebar

  1. reaally nice post! enjoy coming to wpmu.org everyday..theres always something useful! :)

  2. 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

  3. Sarah,
    I hope you don’t mind, but I took your tutorial and widget-ized and made into a plugin. I gave you and WPMU.org credit throughout plugin. You can read all about, and download it here.

  4. Pingback: Buddypress Widgets Pack plugin | Damon Cook

  5. 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

  6. Great tutorial! Could you display the total comments a user made the same way? that would be great.

Click on a tab to select how you'd like to leave your comment

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>

Subscribe without commenting