ALT and TITLE tags for Gravatars in WordPress

label_outlinechat_bubble_outline Comment

Optimizing your site for SEO and overall html5 compliance requires that you have ALT, and preferably TITLE, tags for all images displayed on the site. In WordPress, by default, it grabs Gravatars (a service for providing globally unique avatars) and places those into the posts and comments on your site. What it doesn’t do is give those images the tags that we need to become compliant and perform well on SEO audits.

When we first discovered this issue we searched online and found that it was previously discussed by Ian Gough in his post Setting up the ALT tag for Gravatar in WordPress. His approach was to use the get_comment_author($id_or_email); pluggable. If we were going to use this we would prefer to use the get_comment_author( ‘ID’ ); approach but that’s not the point.

On this site and many other sites we use Disqus (a blog comment hosting service for websites and online communities) which automatically places these alt tags on the images. Therefore, we don’t need to worry about comment author images and tags. If we were to use Ian’s approach we would receive and ‘Anonymous’ ALT tag and no TITLE tag for the Gravatars on the posts. This is because we would be looking for the Comment Author’s information. What we want to look for is the post and forum author.

So in order to achieve the ALT and TITLE tags we would simply need to put this in our functions.php file:

function replace_content($text)
{
$alt = get_the_author_meta( 'display_name' );
$text = str_replace('alt=\'\'', 'alt=\'Avatar for '.$alt.'\' title=\'Gravatar for '.$alt.'\'',$text);
return $text;
}
add_filter('get_avatar','replace_content');

Save the file and then you’ll see that your images are now compliant!

From all of us at WireFlare we ask that you help others find the answers they are looking for. Please leave a comment or share this post!

About

Blog Bio Picture For Todd

I'm the President of WireFlare. I have a passion for creativity, online business and internet security. I strive to create a community that empowers people to be themselves. I'm an adventurist, fun loving and caring. Find me hiking in places most people don't dare to go!

Get a free consultation today!