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”.

Fix: WordPress Sitemap Error: “XML declaration allowed only…”

Have you seen a error similar to the one below while trying to access your WordPress Sitemap?

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

Wordpress sitemap.xml error

I’ve looked around and one guy solved the problem by removing some PHP-code that indicates the PHP code ending or whatever you call it “?>” in his themes functions.php file.

Because apparently he had 2 of them which caused the sitemap error for him and someone else too in that thread.

But that was not the problem for me. The problem was because some idiot (me) had made a plugin and left linebreaks and spaces in the plugin PHP-file.

So check your WordPress plugins. Deactivate one at a time and hit F5.

Have fun! 😉