If you have run an SEO audit on your site you may have noticed you get a few warnings for missing alt tags on your Gravatar images, well that is probably why you are reading this article anyway! This is a very common problem and one that I’m sure WordPress will address in a future update. Until then we have made this quick guide to show you have to add useful alt tags to your users Gravatars by using just a simple function.

This guide is very simple but will require you to have some knowledge of PHP, you won’t need to make any changes to the default function so you can just copy and paste. We recommend you place the function below in the child theme functions.php, a must use a plugin or use WordPress Code Snippets.

The Function to add Alt tags to Gravatar images

So this is the function you need to use to add alt tags to your users Gravatar images. The function uses the user’s display name to add a relevant alt tag to the Gravatar, in this case, the alt tag would be “Avatar for Adam” as my display name is ‘Adam’.

 * Gravatar Alt Fix
 * https://wphelper.site/fix-missing-gravatar-alt-tag-value/
function gravatar_alt($text) {
    $alt = get_the_author_meta( 'display_name' );
    $text = str_replace('alt=\'\'', 'alt=\'Avatar for '.$alt.'\' title=\'Gravatar for '.$alt.'\'',$text);
    return $text;

If you need help with this function get a quote for this task today.