WelcomeSparroWeb

Sparrows
0 Default Comment Avatars Unveiled

Default Comment Avatars Unveiled

The search for custom solutions, as I’ve developed this custom theme, has been ongoing and exhaustive. For the most part, searching for what I’m looking for has produced an abundance of results and my task has simply been weeding through to find the optimal solution. Not so when today, as I rounded out comment styling, I searched for answers about where the little avatar images were coming from. Though I’m not particularly sure how the Gravatar has escaped me to date, I can now boast proud ownership of three unique avatars for the three legit email addresses that I use with regularity. Go to gravatar.com to get one.

A second issue that seemed, initially, too complicated, occurred when I tried to figure out how to set a default avatar to something other than the default options available in WordPress (found in Settings>Discussion). I implemented several unique plugins with no success and it began to appear as though I were going to need to edit my trunk files, which I have, so far and thankfully, avoided. I finally stumbled on a bit of code in WordPress.org support that did the trick. While this solution won’t be optimal for non-technical admins, I found it superior to the handful of plugins that were out there (nudge-nudge to folks who can build solid plugins).

First things first. The default comment avatar image that will display in a WordPress post comment is whatever Gravatar a user/commenter has registered corresponding to the email address they are using to leave the comment. I can understand how this is mildly annoying to rely on a third party for this aspect of your custom WordPress site, but I think, after attempting many of the alternative solutions, that it’s really not so bad. If your user doesn’t have a Gravatar, then the default avatar is used. If the author/moderator/other admin wishes to leave a reply comment, the Gravatar associated with the email address they have assigned in their WordPress profile will be used. If they don’t have one, then the default image will also be used.

So how to customize that pesky default image? You could try a plugin – there’s several that are supposed to work seamlessly out there. However, the solution that worked best for me was to create a functions.php file and put the following into it:
if ( !function_exists('fb_addgravatar') ) {
function fb_addgravatar( $avatar_defaults ) {
$myavatar = get_bloginfo('template_directory') . '/images/avatar.jpg';
$avatar_defaults[$myavatar] = 'Doc4';
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'fb_addgravatar' );
} ?>

You could also add the string to your existing ‘functions.php’ file, of course. I just didn’t have one yet since the need hadn’t arisen during the course of creating my custom theme.

Once I uploaded my new ‘functions.php’ file to my theme folder, I needed to make sure the corresponding image was also uploaded. You’ll notice by the path in the code above that I placed the avatar image in my main images directory: ‘/images/avatar.jpg.’ The default size for these images is 80px x 80px. Once the image was uploaded, I logged back into my WordPress admin and went to Settings>Discussion and found my avatar was among the ranks of existing WordPress default avatars! I selected the radio button next to it, updated, and, my custom avatar now appears next to comments of non-Gravatar commenters.

Leave a Comment