Custom Gravatar not working

I’m trying to upload a custom Gravatar to WordPress, however something is breaking the image link and I’m not sure how to fix it.

add_filter( 'avatar_defaults', 'new_default_avatar' );

function new_default_avatar ( $avatar_defaults ) {
    //Set the URL where the image file for your avatar is located
    $new_avatar_url = get_bloginfo( 'template_directory' ) . '/img/icons/avatar.jpg';
    //Set the text that will appear to the right of your avatar in Settings>>Discussion
    $avatar_defaults[$new_avatar_url] = 'New Default Gravatar';
    return $avatar_defaults;
}

The code works, because I get a radio button with the option to add the Gravatar. The only thing that is broken is the actual image.

When I right click on the image I get this URL:

http://i2.wp.com/localhost/wordpress/wp-content/themes/blog-theme/img/icons/avatar.jpg

I’m not sure where i2.wp.com is coming from, or what it’s doing. When I remove that and do

loalhost:888/wordpress/wp-content/themes/blog-theme/img/icons/avatar.jpg

I can see the Gravatar image.

When I googled this, I saw a lot of people have this problem because of the Jetpack and Photon plugins, but I do not have any plugins installed.

Solutions Collecting From Web of "Custom Gravatar not working"

I think the problem is simply that your default avatar isn’t publicly available.

Try for example

http://img.mywordpresstips.com/gravatar/Avatar-Teaser-Poster.jpg

instead of

http://img.mywordpresstips.com/gravatar/avatar.jpg

Notice that the default gravatar is served via http{|s}://i{0,1,2}.wp.com/...

From the docs:

When you include a default image, Gravatar will automatically serve up that
image if there is no image associated with the requested email hash. There are a > few conditions which must be met for default image URL:

  • MUST be publicly available (e.g. cannot be on an intranet, on a local
    development machine, behind HTTP Auth or some other firewall etc). Default
    images are passed through a security scan to avoid malicious content.
  • MUST be accessible via HTTP or HTTPS on the standard ports, 80 and 443,
    respectively.
  • MUST have a recognizable image extension (jpg, jpeg, gif, png)
  • MUST NOT include a querystring (if it does, it will be ignored)

This is because of Photon addon you enabled in JetPack plugin. That’s why your image URLs will be like…

> http://i0.wp.com/image-url
> http://i1.wp.com/image-url
> http://i2.wp.com/image-url

You will get any of these three URLs. It’s WordPress’s free CDN services. Which is great.

The problem is that you are trying it on localhost that’s why it’s not working. Because WordPress CDN or any other CDN can not pull images from localhost. You should try it on online server. Just disable photon and everything will be fine 🙂

Word of advice, never turn on CDN services on localhost. You will get into errors.

EDIT: By default it should have something like…

http://0.gravatar.com/avatar/...

But in your case it’s using http://i2.wp.com/ which must be caused by a plugin or you added some code in functions.php.

Did you added code in functions.php to return all images from http://i2.wp.com/. This must also be a case.