🚀 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 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:

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

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

「Naibabiji WordPress Website Building Coaching」 — From selecting a domain and purchasing hosting to installing themes and publishing posts, I「ll guide you through every step, helping you avoid detours and reach your goals directly.

👉 Learn about 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 to Follow