Site icon WP Smith

How to Add an Avatar To WordPress Defaults, Remove Default Avatars, & Set Your Default

With WordPress and Genesis, Avatars are used as identification (like the Twitter icon), and with Gravatar it is in the comment area and the User Profile Widget.

Default Avatars
Click for larger image

WordPress allows the user to set a default or generated Avatar, if the commentator doesn't have one. However, I find these options rather ugly or not fitting to the theme most of the time.

Add an Avatar to WordPress Defaults

So I'd like to show you how to add an avatar to the WordPress defaults.
[php]
// Add Custom Avatar (Discussion Settings)
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
$new_avatar = get_stylesheet_directory_uri() . '/images/genesis-48x48.png';
$avatar_defaults[$new_avatar] = "Genesis";

return $avatar_defaults;
}
[/php]

Click for larger image

Remove Default Avatars from WordPress Defaults

For those of us who don't care for some of the WordPress defaults, within that function you can easily remove them at your will:
[php]
// Add Custom Avatar (Discussion Settings)
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
// Get Avatar from child theme images folder
$new_avatar = get_stylesheet_directory_uri() . '/images/genesis-48x48.png';
$avatar_defaults[$new_avatar] = "Genesis";

// Remove default avatars
unset ( $avatar_defaults['mystery'] );
//unset ( $avatar_defaults['blank'] );
//unset ( $avatar_defaults['gravatar_default'] );
//unset ( $avatar_defaults['identicon'] );
//unset ( $avatar_defaults['wavatar'] );
//unset ( $avatar_defaults['monsterid'] );
//unset ( $avatar_defaults['retro'] );

return $avatar_defaults;
}
[/php]

Click for larger image

Set Your Default

Now, WordPress gladly will set your default avatar to the well-known, popular Mystery Man. However, instead of navigating to Settings > Discussion, you can easily set your default as well.
[php]
// Set new avatar to be default
add_action ( 'admin_init' , 'wps_avatar_default');
function wps_avatar_default () {
$default = get_option('avatar_default');
if ( ( empty( $default ) ) || ( $default == 'mystery' ) )
$default = get_stylesheet_directory_uri() . '/images/genesis-48x48.png';
update_option ( 'avatar_default' , $default );
}
[/php]
The known downside of this is that now, Mystery Man can never be chosen so long as this code is active (so it would work well to remove Mystery Man (unset ( $avatar_defaults['mystery'] );) above. This way you can easily control the avatars on your site.

Click for larger image