Show custom default avatar instead of gravatar

2019-08-29 14:13发布

问题:

I'm using this custom function to get the default avatar from my server instead of gravatar:

if(!function_exists('custom_avatar')){
    function custom_avatar($avatar_defaults){
        $new_default_icon = 'http://localhost/gv/wp-content/images/mystery-man.png';
        $avatar_defaults[$new_default_icon] = 'Custom Avatar';
        return $avatar_defaults;
    }
    add_filter('avatar_defaults','custom_avatar');
}

but the custom avatar is not showing up, when I view source code then the src of the image look like this:

http://0.gravatar.com/avatar/a432e8915b383edd8d25c2a4fd5a6995?s=32&d=http%3A%2F%2Flocalhost%2Fgv%2Fwp-content%2Fimages%2Fmystery-man.png%3Fs%3D32&r=G&forcedefault=1

Why does my image src relative to gravatar here? How can I fix this problem?

回答1:

From 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)

So in your case you need to put an image not to http://localhost/ but to some public host.



标签: php wordpress