You are reading

How to Set a Custom Gravatar Image in WordPress

Nearly every blog shows avatars next to the visitors’ comments. They add some variety between different comments and make the discussion more personal. Since version 2.5 WordPress has Gravatars built-in and you don’t need any additional plugin for basic usage and management. If the visitor’s email address has no associated gravatar, the default gravatar image is displayed. I probably won’t be wrong to say that “Mystery Man” is the most popular one from the defaults since it is suitable for most audiences and looks professional. However, it is used in so many blogs and it doesn’t add any uniquenes or help you to brand your blog more. So why not to change the default gravatar to something more personal especially when you can do it only with a few extra lines of code. In this quick and simple tutorial I will show you how to do this.

First, open the functions.php file in your active theme directory. If there’s no such file, then create it. Then add this piece of code:

<?php
function my_custom_gravatar($avatar_defaults) {
    $my_avatar = get_template_directory_uri() . '/images/my_avatar.png';
    $avatar_defaults[$my_avatar] = 'My Custom Gravatar';
    return $avatar_defaults;
}
add_filter('avatar_defaults','my_custom_gravatar');
?>

The location of your custom avatar image file is specified in the first line inside the function my_custom_gravatar. Change it according to your needs. The title My Custom Gravatar defined in the second line inside the function will be displayed next to your custom avatar image in WordPress backend. Change it to whatever you want.

After this a new option in the WordPress backend under Settings → Discussion will appear:

Default Gravatars

Click on it, save the changes and from now on your custom avatar will appear next to the comments left by visitors without a Gravatar.

I hope you enjoyed this simple tutorial and find it useful!

Follow us on Twitter, or subscribe to MazeofMinds RSS Feed for more tutorials and posts.

Leave a Reply

Your name is required
Your email is required
Optional
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>