How To Add Custom Default Avatars

The WordPress mystery man is infamous with WordPress, a huge number of blogs have him as the default avatar and while he has his own attraction it can look more polished using your own custom avatar.

Today I’m going to run through the easy way of doing this, you will be working in the functions.php file so please be sure to back it up before editing.

Before we start you will need to create your new avatar image in your image creation tool of choice (eg. Photoshop.) Size it to the same dimensions as the other avatars/gravatars in your comments page.

Then upload the image file to the images folder of the template your are using.

Open functions.php and place the following:

if (!function_exists('fb_addgravatar')) {
function fb_addgravatar($avatar_defaults) {

// alter file name and folder name if needed
$newavatar = get_bloginfo('template_directory').'/img/avatar1.jpg';
$avatar_defaults[$newavatar] = 'new-avatar';

return $avatar_defaults;
}
add_filter('avatar_defaults', 'fb_addgravatar');
}

Now when you visit the Settings>Discussion section in your admin panel you will see your new default avatar, as seen below.

avatar

If one isn’t enough you could add a number of custom default avatars by slightly altering the code snippet above, for example for two images.

Open functions.php and place the following:

if (!function_exists('fb_addgravatar')) {
function fb_addgravatar($avatar_defaults) {

// alter file name and folder name if needed
$newavatar = get_bloginfo('template_directory').'/img/avatar1.jpg';
$avatar_defaults[$newavatar] = 'new-avatar';

// alter file name and folder name if needed
$newavatar2 = get_bloginfo('template_directory').'/img/avatar2.jpg';
$avatar_defaults[$newavatar2] = 'new-avatar-2';

return $avatar_defaults;
}
add_filter('avatar_defaults', 'fb_addgravatar');
}

We now have two avatars to chose from as you can see below, if you wish you can add more.

avatar

Once selected as a custom default avatar we can now see them on the comments page for example:

avatar

Conclusion

This is a really simple hack that will add further customization to your WordPress install.

You Might Also Like