Setting up the ALT tag for Gravatar in WordPress

Posted By: Ian on Dec 10, 2012 in Blog
Last modified on January 7th, 2013 at 11:52 am,

Today whilst surfing the net i came across this site which had a post which matched exactly what i was wanting todo which was setup the Alt tag for gravatar to rid my site of “missing or empty alt tag” warnings when testing with certain tools.

The site above did not fix this for me but did put me in the right direction and now i’m pleased to share my fix here.

Updated 27/12/2012 with function instead of hack.
Place the following in your themes functions.php instead of the hack in red below as modifying core wordpress files is not recommended and any changes made could be lost when updating wordpress itself.

function replace_content($text)
$alt = get_comment_author($id_or_email);
$text = str_replace('alt=\'\'', 'alt=\''.$alt.'\'',$text);
return $text;

So head to “wp-includes/pluggable.php” and search for the line

$safe_alt = '';

and replace it with  

$safe_alt = get_comment_author($id_or_email);

Now instead of an empty Alt tag accompanying all your comments the comment authors name will be displayed there instead.

See for other options to display instead of the comment authors name like say there link if they provide one.

Hope this helps,


Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

Discussion - 7 Comments

  • Russ Benk Russ Benk Dec 11, 2012 

    Thanks a lot for tracking me down and pointing me to this answer.

    • Ian Ian Dec 27, 2012 

      Hey Russ,
      No problem Please see updated code with function instead of hack.
      All the best,

  • Axel Schröder Axel Schröder Jul 13, 2013 


    thanks a lot for the snippet. Is it able to add the title-tag for the gravatar-url also?


  • Todd Todd Sep 10, 2013 


    Thanks for the great post. We’ve created a different version that can be used for authors while leaving the comment avatars up to Disqus. Your post, and a little SEO optimization, is what inspired us to take this route. In our post we also included a fix for the title tag as suggested by Axel.

    If anyone’s interested in our version here’s the link:

    ALT and TITLE tags for Gravatars in WordPress

    Thanks again for the great post!

  • Chris Dill Chris Dill Dec 27, 2013 

    You can also add a title attribute,

    add_filter(‘get_avatar’, ‘add_avatar_attributes’, 10, 5);
    function add_avatar_attributes($avatar, $id_or_email, $size, $default, $alt){
    $title = get_the_author_meta(‘display_name’);
    $doc= new DOMDocument();
    $img = $doc->getElementsByTagName(‘img’)->item(0);
    $img->setAttribute(“title”, $title);
    return $doc->saveHTML();

  • david david Feb 11, 2015 

    Thank you very much,
    i was looking for this, sometimes, the best solution it’s the simplest.


leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,

protected by copyscape duplicate content check


Powered by Banner Bar 125