Amber Weinberg: Freelance Web Developer specializing in semantic WordPress, Mobile, CSS and HTML5 Development

The Blog

How To Add BuddyPress Avatars To WordPress MU

Posted on 02/04/10 in blog, development about , , ,

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

About the author
Amber Weinberg specializes in clean and semantic XHTML, CSS and WordPress development. She has over 10 years of coding experience and is pretty cool to work with. Amber is available for freelance work, so why not hire her for your next project?

11 Awesome Comments

  1. Andrea_R says:

    BP is still relatively new and up till now has only worked on MU. the docs are mainly user-contributed, and there’s been a LOT of internal changes, so that;s why the docs are sparse.

    your best bet is the buddypress docs on the official site, and the forums there.

  2. I’ve used BuddyPress once before last year shortly after the release but not since then. The docs are a little hard to come by but I do see a great future for BP if development keeps going strong and docs are written.

    Amber, have you looked at doing some syntax highlighting to your code blocks on posts? Would be a huge improvement for posts like these. Could check out this plugin: http://wordpress.org/extend/plugins/wp-syntax/ There are also other methods and jQuery plugins, etc.

  3. Anyeos says:

    Hey, I’m trying to figure out of how to have the same BP avatar in all the MU weblogs because when I open another user weblog I need to put another avatar for my own account. I don’t understand why each blog force me to put a new avatar. What is that? Any kind of bug? or it is just a “feature”? I prefer to have the same avatar globally in all weblogs in the same WPMU domain!

    • Most BuddyPress sites like to use BuddyPress avatars which, unlike Gravatar, are not global, but specific to each BP site. It’s done on purpose, although I dislike it as well. The good BP sites are supposed to default to your Gravatar if you don’t have a BP Avatar set.

    • Anyeos says:

      Ok, I just noticed what BP_AVATAR_URL and BP_AVATAR_DIR are setted relative to url and dir of user blog. I reeplaced that code for one what always use the same absolute paths (obtained from WP_CONTENT_URL and WP_CONTENT_DIR) using the global “uploads” directory. Now all avatars are the same in all weblogs.
      If you like to do it edit the file bp-core/bp-core-avatars.php and modify this functions:

      function bp_core_avatar_upload_path() {
      $basedir = WP_CONTENT_DIR.’/uploads’;
      return apply_filters( ‘bp_core_avatar_upload_path’, $basedir );
      }

      function bp_core_avatar_url() {
      $baseurl = WP_CONTENT_URL.’/uploads’;
      return apply_filters( ‘bp_core_avatar_url’, $baseurl );
      }

      :)

  4. rohn says:

    Please help.

    Cannot make out exactly in which files to put these codes.

    For example: exactly in which file and exactly where do I put

    I just want that users who have uploaded a cropped photo or avatar via buddypress should be able to show the same in all posts and comments across blog posts in wordpress mu. Those who have no such upload or are non-registered commenters should have gravatars. I wish this was an easy option aotomatically available.

    • Anyeos says:

      The gravatar is already automatically showed in all posts/blogs/comments whatever the user with that email address is using in her/him gravatar.
      For making work the BP included avatar feature in all posts/blogs/comments etc you must modify the file bp-core/bp-core-avatars.php and reeplace the functions what I was said in my above comment (I already said that).

      What the post owner are saying here is already included in BuddyPress. I just don’t modify nothing of that to show the avatars in the comments/posts/etc it is working out of the box. But in the same blog, when I go to another blog I need to newly upload an avatar (I can upload another different) and when you see my posts in that blog you will see that avatar. But when you see my posts in another blog (of the same WPMU) you will see another avatar (the avatar what I was uploaded in that blog). So out of the box BuddyPress support avatars for all blogs/comments/etc but they are independet for each blog. I don’t liked that “feature”. I wanted the same avatar in all blogs/posts/commets. So I decided to solve that for myself and the fix is what I was already explained.

      Try to test my fix.

      Byebye

  5. rohn says:

    For example: exactly in which file and exactly where do I put

    global $bp
    echo get_avatar( get_the_author_id(), ’37′ );

  6. Anyeos says:

    Excuse me @Amber. I was in some mistakes.
    That Amber is telling here is about modifying another places of WordPress MU not the features already included in BP.
    She talks too about a “Global Comment Plugin”.
    Anyway, @rohn, you don’t need to modify what Amber are telling here. That is for customize some things. BuddyPress as is right now give you what you need with the exception of my fix.

Leave a Reply