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; } add_filter('get_avatar','gravatar_alt');
If you need help with this function get a quote for this task today.
Great It’s Working Properly
Im using sahifa theme, could you please guide the file name to edit the above code?
Where are you supposed to add this code? Does it go inside Gravatar.com or inside your WordPress blog? Thanks m
Copy and paste it into your functions.php.
Thank you so much man, It’s worked.