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 redd-design.com 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;
}
add_filter('get_avatar','replace_content');

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 http://codex.wordpress.org/Function_Reference/comment_author for other options to display instead of the comment authors name like say there link if they provide one.

Hope this helps,

AnonymousIan.J.Gough

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

Discussion - 6 Comments

  • Russ Benk Russ Benk Dec 11, 2012 

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

    Reply
    • Ian Ian Dec 27, 2012 

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

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

    Hi,

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

    Best,
    Axel

    Reply
  • Todd Todd Sep 10, 2013 

    Ian,

    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!
    Todd

    Reply
  • Chris Dill Chris Dill Dec 27, 2013 

    You can also add a title attribute, http://stackoverflow.com/questions/20794497/wordpress-how-do-i-add-extra-html-tags-to-existing-wordpress-functions

    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();
    $doc->loadHTML($avatar);
    $img = $doc->getElementsByTagName(‘img’)->item(0);
    $img->setAttribute(“title”, $title);
    return $doc->saveHTML();
    }

    Reply

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,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125