🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

Add ALT Attribute to Gravatar Avatar Images in WordPress Comments

Today, while doing SEO analysis in the Bing Webmaster Tools backend, it prompted that the website images lack alt information and suggested adding it. After checking, I found that 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 backend forFix Gravatar Alt Text & Title Tag, then install it.

The final effect is as follows:

https://wordpress.org/plugins/fix-gravatar-alt-text-title-tag/

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

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.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: