2

I am looking for a solution to output nothing instead the default avatar when a user does not have any avatar.

I am currently using this line. I looked at the function and the only thing I got so far is to use blank as default avatar but its still occupies space and I do not want that.

echo get_avatar( get_the_author_meta( 'ID' ), 70, 'blank', __( 'avatar', 'bla' ) );

Sidenote: Even that blank avatar gets pulled from Gravatar, I think this is ridiculous.

0

Unlikely to be worth your time to implement such a thing. You need to ask gravatar to give you the URL for the image associated with the email and inspect if it is the blank one (or IIRC you can give an alternative default image in the URL). Therefor in any case you will have to do the round trip to the gravatar server, but if you insist on just not showing anything, you will do it from your server instead of the browser, slowing down your page generation.

An easier approach would probably be to hide the blank one with JS.

0

You can use the get_avatar filter to change the output or avatar_defaults to add new image that can be placed on your server.

Here is an example code for adding new avatar that you can set as default from the Settings > Discussion page.

add_filter( 'avatar_defaults', 'add_new_gravatar_image' );
function add_new_gravatar_image($avatar_defaults) {
    $myavatar = 'http://yoursite.com/image.png';
    $avatar_defaults[$myavatar] = "Default Gravatar";

    return $avatar_defaults;
}

And if you want to change the output, you have an example on the documentation page for get_avatar.

0

You can use a function to check if the user has Gravatar set:

function wps_281473_has_gravatar($email) {
    $hash = md5(strtolower(trim($email)));

    $url = 'http://www.gravatar.com/avatar/'.$hash.'?d=404';
    $headers = get_headers($url);

    return preg_match("/200/", $headers[0]) == 1;
}

So your code would be:

if (wps_281473_has_gravatar(get_the_author_meta('email'))) {
    echo get_avatar( get_the_author_meta( 'ID' ), 70, 'blank', __( 'avatar', 'bla' ) );
}
  • 1
    It should be strongly noted that doing a network request is massively slow on a scale of normal PHP runtime. If this technique is used the checks must be heavily cached for reasonable performance. – Rarst Sep 29 '17 at 14:16
0

Gravatar is a remote service. Most ways to do this involve ramping up communication with that remote service with considerable performance hit due to network requests.

You'll have to bend backwards to implement that communication in a way that doesn't ruin your page generation performance.

Ability to provide a default image is reasonable compromise here. If you want complete control over avatars you can as well roll your own local avatar system. There are just inherent limitations to relying on third party service for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.