Force users to display default Gravatars

Is it possible for a WordPress site to display the users default Gravatar image (of chosen set, eg Retro, Identicon, etc), rather than the users uploaded avatar? As in, all user avatars next to comments would be a generated ‘Retro’ picture, regardless of whether they had a custom avatar at Gravatar or not.

Screenshot of the settings in question:
WordPress Screenshot

Read More

Since these default avatars are generated based on the email of the user, I was wondering if it is still possible to generate them, or have Gravatar return these default generated avatars rather than the users custom avatar.

Related posts

Leave a Reply

3 comments

  1. Yeah it is possible.

    Just add a function to the get_avatar hook

    function change_avatar($avatar, $id_or_email, $size, $default, $alt) {
        return 'url_to_your_retro_image';
    }
    
    add_filter('get_avatar', 'change_avatar', 10, 5);
    
  2. Found you can create the address manually to return the image from Gravatar, and pass in parameters to force default and theme. Combined this in part with @Sudar’s answer, as well as the code from pluggable.php as the admin page will grab the ID not email from $id_or_email, so a check needed to be added so the admin page shows the correct avatars.

    function custom_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
    
        if ( is_admin() ) {
    
            $email = '';
            if ( is_numeric($id_or_email) ) {
                $id = (int) $id_or_email;
                $user = get_userdata($id);
                if ( $user )
                    $email = $user->user_email;
            } elseif ( is_object($id_or_email) ) {
                // No avatar for pingbacks or trackbacks
                $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
                if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
                    return false;
    
                if ( !empty($id_or_email->user_id) ) {
                    $id = (int) $id_or_email->user_id;
                    $user = get_userdata($id);
                    if ( $user)
                        $email = $user->user_email;
                } elseif ( !empty($id_or_email->comment_author_email) ) {
                    $email = $id_or_email->comment_author_email;
                }
            } else {
                $email = $id_or_email;
            }
    
            $md5address = md5( strtolower( trim( $email ) ) );
    
            return '<img width="'.$size.'" height="'.$size.'" src="http://www.gravatar.com/avatar/' . $md5address . '?d=retro&f=y" />';
    
        }
    
        else {
    
            $md5address = md5( strtolower( trim( $id_or_email ) ) );
    
            return '<img width="'.$size.'" height="'.$size.'" src="http://www.gravatar.com/avatar/' . $md5address . '?d=retro&f=y" />';
    
        }
    
    }
    
    add_filter('get_avatar', 'custom_get_avatar', 10, 5);
    

    Read more:

    http://en.gravatar.com/site/implement/hash/

    http://en.gravatar.com/site/implement/images/

  3. Nowadays it’s much easier thanks to get_avatar_data() which is being called by get_avatar():

    function wpse75380_force_default_avatar( $args ) {
      $args['force_default'] = true;
      return $args;
    }
    
    add_filter( 'get_avatar_data', 'wpse75380_force_default_avatar' );