How To Add BuddyPress Avatars To WordPress MU

Not many people know what BuddyPress is ( I sure didn’t), so let me start off by pulling a quote from their own site:

“BuddyPress is a suite of WordPress plugins and themes, each adding a distinct new feature. BuddyPress contains all the features you’d expect from WordPress but aims to let members socially interact.”

A little ambiguous but I’m sure you get the point. I recently had a client running BuddyPress on WordPress MU who wanted to replace all their Gravatars to pull BuddyPress Avatars instead. To be honest, when he asked I didn’t even know what BuddyPress is was, let alone what BuddyPress Avatars were.

An extensive Google search (BP has little documentation on the web, which I found odd given that regular WP has the best documentation of any platform…) told me their avatars are tied to an ID (not an email like Gravatar) and are what users specifically upload to the site they’re displaying it on. Another Google search only lead me to one, yes that’s one, blog post on how to add BP Avatars to a site, and that was only for the comments loop.

So what if you’re needing to know how to do this? BuddyPress and WordPress MU is a whole other ballgame when it comes to web development, even though their admin and front-end looks almost exactly the same. It’s definitely worth checking out though before a client asks you do modify one.

Let me show you 3 ways I figured out how to add BP Avatars, depending on your need and situation.

Adding BuddyPress Author Avatars

This one is the simplest of the 3, simply add:

<?php global $bp; ?>
<?php echo get_avatar( get_the_author_id(), '37' ); ?>

Change the number in the second PHP call (37) to the size of the thumb you’re wanting. This, of course, needs to be in the regular post loop. Yes it was that easy…the others however….

Adding BuddyPress Avatars to WordPress MU’s Global Comments Plugin

This is a stock plugin that comes with WordPress MU, that I hacked to call the BP Avatars. This took me a few hours to figure out, so I’m here to save you the same pain.

Around line 137, after it the call for showing recent-global-comments-avatars, replace what is there with this similar code:

if(!empty($comment['comment_author_user_id'])){
$id_or_email = $comment['comment_author_user_id'];
} elseif ( !empty( $comment['comment_author'] ) ) {
$id_or_email = $comment['comment_author'];
} else {
$id_or_email = $comment['comment_author_email'];
}
echo '<a href="' . $comment['post_permalink'] . '">' . get_avatar( $id_or_email, $options['recent-global-comments-avatar-size'], '' ) . '</a>';
echo ' ';
}
echo substr( strip_tags( $comment['comment_content'] ), 0, $options['recent-global-comments-content-characters'] );

Although as you’ll see, this code is almost exactly the same as the default code, it actually changes it to call the ID of the commenter, instead of the email address.

Adding BuddyPress Avatars To WooThemes Recent Blog Comments Tabs

You’d think this would be relatively the same as above, but the regular recent blog comments that comes in a lot of BuddyPress themes doesn’t call the commenter’s ID from the database, so it’s impossible just to “switch it out” like in the last example. This bit of code can be modified easily to make your own widget or to replace any recent comments display.

<?php
$comment_posts = get_option('woo_comment_posts');
if (empty($comment_posts) || $comment_posts < 1) $comment_posts = 5;
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,50) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC LIMIT ".$comment_posts;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$sql2 = ' select ID from wp_users where user_email = "' . $comment->comment_author_email . '"';
$userData = $wpdb->get_results($sql2);
//print "<pre>" . print_r(array($userData, $sql, $sql2, $comment)) . '</pre>';
?>
<li>
<?php print get_avatar($userData[0]->ID); ?>
<a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="on <?php echo $comment->post_title; ?>">
<?php echo strip_tags($comment->comment_author); ?>: <?php echo strip_tags($comment->com_excerpt); ?>...
</a>
<div style="clear:both"></div>
</li>
<?php
}
?>

This will display the avatar and comment in a nice list that’s linked to the post it came from.

Your Thoughts

Have you worked with BuddyPress or WordPress MU before? What did you think about them? Did you have the same problem with finding documentation as I did?

Main image by jared, logo by BuddyPress