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.
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]
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]
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.
Will Jackson says
Hi – I couldn’t get your gravar replacement code to work, the image was displaying as broken, and was still referencing the gravatar url – shame as this would be a really useful technique, sorry I can’t offer a fix.
Stijn says
I just found out that the gravatar image should be available online. Had trouble with it locally myself, ran into the same issue. So i uploaded the image, reference the publically available location and the image shows.
Mike Hemberger says
Same issue as Will Jackson here. Strange thing is, I’ve used this code before.. this time it’s not working though.