Wrong Avatar and Gravatar after BP update

Whenever someone leaves a comment on a blog the avatar/gravatar that’s fetched every time is the blog author’s avatar. Even if the commenter isn’t even logged into the site and doesn’t even have a gravatar account the comment will still display the blog author’s comment.

Here’s an example. As you can tell it looks kind of weird with 10 comments from different people but all with the same avatar.

So I’ve been narrowing down the problem all night and here’s what I know:

1. This problem occurred after upgrading to BP 1.2.1 . Whenever I disable all plugins on the site the problem persists. But then when I also disable Buddypress the problem stops and just shows gravatars. So I know it can’t be a WPMU problem.

2. So I completely deleted BP 1.2.1 and manually reinstalled BP 1.2.1 but still no luck. Then I noticed that some themes that I had uploaded after upgrading to BP 1.2.1 displayed the gravatars and avatars correctly. Whether I choose the themes from the WordPress repository, download them somewhere else online, upload via the “New Themes” option, or via FTP they all display avatars and gravatars correctly.

3. The only problem is with my pre-existing themes before the update. So I tried deleting a few and then uploading them again but with no luck.

It’s only remotely similar. Our main blog is fine… but that’s because it’s using a theme that was added after the BP 1.2.1 install (or rather, it was added <i>by</i> the BP 1.2.1 install – we’re using the default BP 1.2 theme).

All new themes added after the install work correctly, it’s only the pre-existing themes which have problems… which means that everyone is still using the old themes and I can’t exactly tell them to switch themes right away.

Alright, one other anomaly. When logged in as the administrator but I create a blog post on someone else’s blog that isn’t mine it shows the avatars/gravatars correctly, but only for that post. All the other blog posts on the blog by the blog owner show the avatars/gravatars incorrectly (i.e. they replace every gravatar/avatar with the blog owner’s uploaded avatar).

Does anyone else have this problem? Or does anyone have a clue as to what’s wrong?

At one point while I was still deciding which theme to use on the front I ran Andy’s BuddyPress Template Pack on another theme but then decided to just stay with the default theme in BP 1.2.1 . It was after that I noticed the gravatar/avatar problem. But maybe the problem existed before then?? I don’t know since I didn’t check.

As I already mentioned I’ve already deleted BP 1.2.1 manually and then manually reinstalled it but still to no avail.

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.