BuddyPress wrong avatar – fix!

This post shows two possible fixes for a common issue in the newest versions of WordPress and the BuddyPress plug-in.

Problem description:

  • Comment section shows the blog author’s avatar next to every comment, instead of the one belonging to the commenter on all blogs except the main blog. All associated emails are correct in the database and being handed off correctly to the get_avatar function.
  • Correct Gravatars show when BuddyPress is not active.

Test system description:

  • WordPress MU version 2.9.x
  • BuddyPress 1.2.2.1
  • Main blog associated with Buddypress uses the default BuddyPress theme.
  • Other blogs used an assortment of non-BuddyPress themes, but BuddyPress is designed and intended to work with any theme on blogs other than the main one.

What I found:

In the file /wp-content/plugins/buddypress/bp-core/bp-core-avatars.php, lines 344 and following contain the following code:

global $authordata;
if ( is_object( $user ) )
$id = $user->user_id;
else if ( is_numeric( $user ) )
$id = $user;
else
$id = $authordata->ID;


if ( empty( $id ) )
return $avatar;

I think that last “if statement” may be there to catch cases where the commenter does not have a BuddyPress account (which is the majority on my site at this point). However, if you look closely at the block just above the “if ( empty( $id))” you will notice $id is never going to be empty at this point.

That suggests two possible solutions.

Solution 1:

I have not tested this, but it makes sense that you could move the last “if statement” above the first “if statement.” That way you can check for “empty( $id )” before it gets set to”$authordata->ID

Solution 2:

I found it works quite well to add these lines just under the last “if statement.”

if ( is_string( $user ) )
return $avatar;

This will catch any case where an email is sent as input, which is what all (most) non-buddypress themes send. Cases where numbers or objects are sent (the BuddyPress method) will be handled normally.

Maybe I can get this added into the next release of BuddyPress.

2 responses to “BuddyPress wrong avatar – fix!

  1. worked a treat thanks

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>