WordPress Gravatar Fallback Image For Recent Comments

March 29, 2015 - Wordpress
WordPress Gravatar Fallback Image For Recent Comments

If you like to use Gravatar images for your users you may find that the default avatar served up when a user does not have a Gravatar account a little ugly. It would be nice if Gravatar let you choose a default avatar to serve by domain, but so far as I can see they don’t.

After scouting around the options I found a few bits of code to do various things so I pulled them all together. I made something that would grab an avatar for people with Gravatar accounts, but serve a default avatar for those that don’t.

The code involves a little PHP and some CSS. Obviously I don’t know how you insert your PHP, everyone probably has different methods so I won’t assume anything. I use Shortcode Exec PHP because I find it a handy little tool for dropping PHP into my site without having to edit source.

So the PHP code first…

You will want to customise the default image on this line :

So upload the image you want to use as a default to your theme’s images folder. Change ‘leaf.png’ to point to whatever you uploaded.

The validate_gravatar function will figure out if the user has a Gravatar to be served. If that does not find a Gravatar then your default one will be shown.

And here’s the CSS :

I added this to my theme via Appearance -> Theme Options -> Layout -> Custom CSS. If your theme does not have that option you could use this Simple Custom CSS plugin. Tweak the CSS to suit your site. As you can see currently I have mine in my footer. Now they are in my side panel as I changed my theme.

(Visited 57 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *