Add a Custom Avatar to your WordPress theme

Internet identity, or internet persona is a social identity that an Internet user establishes in online communities and websites
–Wikipedia

Most people commenting on blogs online have an avatar associated with their online personas. If, however, they don’t and they’re commenting on your blog and you don’t particularly like the WordPress default avatar options, you can define your own.

To do so, include the following code in your functions.php:

if ( ! function_exists('butter_custom_avatar') ) {
  function butter_custom_avatar( $avatar_defaults ) {
    $myavatar = get_stylesheet_directory_uri() . '/images/butter-avatar.png';
    
    $avatar_defaults[$myavatar] = 'Butter custom avatar';
    return $avatar_defaults;
  }
  add_filter( 'avatar_defaults', 'butter_custom_avatar' );
}

What we’re doing here first, is checking to see if the function doesn’t exists and by so doing making the function pluggable—accessible/modifiable in child theme. If it doesn’t exist, we define it and add a filter avatar_defaults that tells WordPress to add our custom defined avatar to the list of available avatars.

We are telling WordPress to find this avatar (defined by the $myavatar variable) inside our “images” directory which is itself inside the theme/template directory. Next step, obviously, is to create the image itself and upload it to said “images” folder.

With that in place, in addition to the default WordPress avatars, you’ll now have another custom avatar that you can select as the new default.

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.