Fix ALT tags for avatars in BBPress (WordPress)

I recently discovered a little “error” on one of my websites running WordPress with BBPress.
The ALT tag for all avatars/gravatars used in my WordPress BBPress forum was set to: alt=””.

Bing, according to their webmaster tools, did complain over the lack of the BBPress avatars ALT tags on most pages in my forum.
Seeing all those error messages annoyed me too much, so I decided to fix it.

BBPress forum ALT tag fix.

So, here how to fix the problem:

You need to edit your themes functions.php file.
Add this bit of PHP code to the bottom of that file:

function filter_gravatar_alt($img) {
   if ( have_comments() ) {
      $alt = 'Avatar image';
   }
   else {
      $alt = 'Avatar image';
   }
   $img = str_replace('alt=\'\'', 'alt=\''.$alt.'\'', $img);
   return $img;
}
add_filter('get_avatar', 'filter_gravatar_alt');

Save the functions.php file and upload it or just update it if you’re using WordPress built-in editor.

Hopefully you’ll see that your BBPress avatars/gravatars contain the ALT tag with the text “Avatar image”.