Create A Custom Default WordPress Avatar

By .     What do you think of this article?

Inside the Discussion Settings area of your WordPress blog, there are seven options for default avatars. The option you select will show up for every user who hasn’t setup a Globally Recognized Avatar for their email address. That is, of course, if you’ve adjusted the Avatar Display option to Show Avatars and your theme supports Gravatars (I haven’t seen a theme that doesn’t). Wouldn’t it be great if you could create a custom avatar and set it as the default option? That’s what we’re going to do today.

First, start off by creating your default avatar. An image with the dimensions 90 width by 90 height is great because most themes don’t use use anything larger than that, so you never have to worry about it getting stretched and ugly. Taking a large image and shrinking it down produces a nice result. The image can be any file type, but I suggest either PNG, JPG/JPEG, or GIF. The file size shouldn’t be too high either. Something under 10 KB is great.

Secondly, upload the default image to your theme’s directory.

Third, open up your theme’s functions.php and add the following code to the end of the file:

/**
  * Add a custom avatar as option in Admin > Settings > Discussion page
  * 90x90 and under 10 KB is a good size; your site will scale it when necessary
  * https://www.computertechtips.net/92/create-a-custom-default-wordpress-avatar/
  */
if ( !function_exists('ctt_defaultavatar') ) {
  function ctt_defaultavatar( $avatar_defaults ) {
    $defaultavatar = 'http://www.yoursite.com/wp-content/themes/yourtheme/images/default-avatar.png';
    $avatar_defaults[$defaultavatar] = 'CTT Default';
    return $avatar_defaults;
  }
  add_filter( 'avatar_defaults', 'ctt_defaultavatar' );
}
  • Lines 1-5 are PHP comments. You can omit lines 3 & 4 to conserve space, but keep the code’s description in line 2.
  • Line 6 checks to see if there is already a function by the name we’ll soon create. If there isn’t a duplicate, it will continue the script.
  • Line 7 creates the ctt_defaultavatar() function based on the $avatar_defaults variable already created in the WordPress core.
  • Line 8 is the direct link to your default avatar. Adjust that line to suit your needs. You can optionally replace that line with the following to automatically add your theme’s URL:
    $defaultavatar = get_bloginfo('template_directory') . '/images/default-avatar.png';
    I prefer hard-coding the link because the default avatar is unlikely to move directories and I just saved from processing an additional PHP command.
  • Line 9 names the avatar among the list of defaults like “Mystery Man” and “Gravatar Logo”.
  • Line 10 ends the function’s execution and returns it as a value for that function to call.
  • Line 12 adds the function we just created to the list of default avatars in the Discussion Settings page

The last step to this tip is to login to your WordPress Dashboard, head over to the Discussion Settings page, and enable your new default avatar.

Now, your theme looks more customized and shows off your personal style. Or, if you used a part of your website’s logo, you’re further advertising your brand to the readers and making it even more recognizable.

Posted in: Web Development



is the site owner of Computer Tech Tips and is passionate about computer technology, particularly Windows-based software, malware removal, and web development. He enjoys helping people troubleshoot computer problems and providing technical support to family, friends, and people around the net. Xps wrote 78 article(s) for Computer Tech Tips.


 Subscribe to comments: this article | all articles
Comments are automatically closed after a period of time to prevent SPAM.