不得不说在国内很多人不习惯使用邮箱,甚至没有 Gravatar 头像,针对国内情况对主题进行了一定的优化。如果使用 QQ 邮箱,则调用 QQ 头像,否则继续调用 Gravatar 头像,这样既方便了 QQ 用户,也方便了 Gravatar 用户。此方法并不会拖慢网页加载速度,可以说是一个比较好的方法了。

Image

首先,我们找到了获取 QQ 头像的接口 https://q.qlogo.cn/g?b=qq&nk=52958812&s=100,然而我们并不能直接获取评论者的 QQ,所以我们需要获取评论者的 email 地址:

get_comment($parent_id)->comment_author_email);

有了 email 地址,进行判断是否为 QQ 邮箱:

if(strpos($qqmail, '@qq.com'))

如果是 QQ 邮箱的话,则调用 QQ 头像:

$avatar_source = 'q.qlogo.cn';
$img = 'g?b=qq&nk=' . preg_replace('/@qq.com/', '', $qqmail) . '&s=100';

否则仍然调用 Gravatar 头像:

$avatar_source = 'cn.gravatar.com';
$img = 'avatar/$1?s=$2';

这样这个问题就愉快地解决了。

完整代码如下:

// Gravatar 头像
function get_avatar_javst($avatar) { 
    $protocol = is_ssl() ? 'https' : 'http';
    $qqmail = trim(get_comment($parent_id)->comment_author_email);
    if (strpos($qqmail, '@qq.com')) {
        $avatar_source = 'q.qlogo.cn';
        $img = 'g?b=qq&nk=' . preg_replace('/@qq.com/', '', $qqmail) . '&s=100';
    } else {
        $avatar_source = 'cn.gravatar.com';
        $img = 'avatar/$1?s=$2';
    }
    $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&amp;.*/', '<img class="avatar avatar-$2" src="' . $protocol . '://' . $avatar_source . '/' . $img . '" width="$2" height="$2" />', $avatar);
    return $avatar;
}
add_filter('get_avatar', 'get_avatar_javst');
如果觉得我的文章对你有用,请随意赞赏