Setting up the ALT tag for Gravatar in WordPress

Posted By: Ian on Dec 10, 2012 in 
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

Hmmm where to start

Discussion - 2 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

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