Today, while performing SEO analysis in the Bing Webmaster Tools backend, it prompted that the website images lack alt information and suggested adding it. Upon checking, some articles have featured images without ALT, and more commonly, Gravatar avatar images lack ALT information. So, I researched how to add ALT information to Gravatar avatar images in the comment section.
Method to add ALT attribute to Gravatar avatar images: Code version
This method is very simple. Just edit the functions.php file of your current Theme and add the following code snippet, then save it.
/**
* WordPress评论区gravatar头像添加alt属性
* 详情介绍:https://blog.naibabiji.com/tutorial/wordpress-gravatar-tou-xiang-jia-alt.html
**/
function gatttf_replace_content( $text ) {
if ( have_comments() ) {
$alt = get_comment_author();
} else {
$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','gatttf_replace_content' );Method to add ALT attribute to Gravatar avatar images: Plugin version
If you are a WordPress beginner and don't know how to add code, you can use the Plugin version. Simply search in the Admin Dashboard forFix Gravatar Alt Text & Title Tag, then install it.
The final effect is as follows:


Comments are closed
The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.