If you’ve ever run a site audit on a WooCommerce store, chances are you’ve seen warnings about images with missing alt attributes. One common culprit that’s easy to overlook: Gravatar avatars in the product reviews section.
By default, WooCommerce renders review avatars with an empty alt=”” attribute. Search engines like Bing flag this as an accessibility and SEO issue and they’re right to.
Why It Matters
Empty alt tags aren’t just a technicality. Search engines use alt text to understand what an image represents. For avatars, a descriptive alt like “Rebecca1988 avatar” tells crawlers this is a user photo tied to a review which reinforces the review’s authenticity signals. It also improves accessibility for screen readers, which is increasingly a ranking factor.
The Fix
Add this snippet to your functions.php (child theme) or via Code Snippets plugin:
add_filter( 'get_avatar', function( $avatar, $id_or_email, $size, $default, $alt ) {
if ( ! empty( $alt ) ) {
return $avatar;
}
$author_name = 'User avatar';
if ( is_object( $id_or_email ) ) {
if ( isset( $id_or_email->comment_author ) ) {
$author_name = $id_or_email->comment_author . ' avatar';
} elseif ( isset( $id_or_email->user_login ) ) {
$author_name = $id_or_email->display_name . ' avatar';
}
} elseif ( is_string( $id_or_email ) ) {
$user = get_user_by( 'email', $id_or_email );
if ( $user ) {
$author_name = $user->display_name . ' avatar';
}
}
return preg_replace( '/alt=["\']["\']/', 'alt="' . esc_attr( $author_name ) . '"', $avatar );
}, 10, 5 );
The snippet handles three scenarios: comment objects (standard WordPress comments), user objects (logged-in user avatars), and raw email strings (how WooCommerce often passes reviewer data). It uses preg_replace instead of str_replace to reliably catch both single and double quote variations in the rendered HTML.
Result
Every review avatar on your WooCommerce store will automatically get a meaningful alt tag the reviewer’s name without any manual work. It works site-wide and requires no plugin.
After adding the snippet, clear your cache if you’re running WP Rocket, Perfmatters, or similar otherwise you’ll still see the old cached HTML with empty alts.


Leave a Reply