Gravatar头像替换国内cdn镜像

前言

最近发现WordPress里面的Gravatar头像打不开了,并且网站也出现了无法读取用户的头像照片,排查原因是secure.gravatar.com竟然在国内打不开了,就算打开也很慢,V2EX有个cdn可以进行替换地址访问。

代码示例

方案一

/*Gravatar CDN替换*/
function getV2exAvatar($avatar) {
$avatar = str_replace(array("www.gravatar.com/avatar","0.gravatar.com/avatar","1.gravatar.com/avatar","2.gravatar.com/avatar","secure.gravatar.com"),"cdn.v2ex.com/gravatar",$avatar);
return $avatar;
}
add_filter('get_avatar', 'getV2exAvatar');

方案二

/*loli的Gravatar CDN*/
function getV2exAvatar($avatar) {
        $avatar = str_replace(array("www.gravatar.com/avatar","0.gravatar.com/avatar","1.gravatar.com/avatar","2.gravatar.com/avatar","secure.gravatar.com"),"gravatar.loli.net/avatar",$avatar);
        return $avatar;
}
add_filter('get_avatar', 'getV2exAvatar');

原理

gravatar.com替换为cdn.v2ex.com/gravatar,实现访问

使用方法

代码示例放在主题模板中的函数文件中(functions.php),清除服务器缓存或浏览器缓存再试

已失效

因v2ex失效,方案一弃用,后期关注开放情况,请使用方案二加速方案。