Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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/

share|improve this answer

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);
share|improve this answer
    
I'm talking about the Gravatar generated retro avatars, one of the default selections under WordPress... It isn't a single image I can return as far as I know? –  user19606 Dec 8 '12 at 17:56
    
I am not sure if I understood it. Can you explain a bit? –  Sudar Dec 9 '12 at 4:05
    
See the screenshot. I want all users to use these Gravatar generated images, rather than their own custom avatar (if they have uploaded one at Gravatar). Another example would be StackOverflow, the difference between my avatar - a generated Gravitar image, and yours - a custom avatar. –  user19606 Dec 9 '12 at 5:02
    
Thanks for the help, I've extended it to include the necessary bits to return the Gravatar generated image in my answer. –  user19606 Dec 9 '12 at 6:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.