WordPress 評論調用 QQ 頭像源
16,002 11
不得不說在國內很多人不習慣使用電子郵件,甚至沒有 Gravatar 頭像,針對國內情況對主題進行了一定的優化。如果使用 QQ 郵箱,則調用 QQ 頭像,否則繼續調用 Gravatar 頭像,這樣既方便了 QQ 用戶,也方便了 Gravatar 用戶。此方法並不會拖慢網頁加載速度,可以說是一個比較好的方法了。

首先,我們找到了獲取 QQ 頭像的接口 https://q.qlogo.cn/g?b=qq&nk=52958812&s=100,然而我們並不能直接獲取評論者的 QQ,所以我們需要獲取評論者的 email 地址:
PHP PHP
get_comment($parent_id)->comment_author_email);
有了 email 地址,進行判斷是否為 QQ 郵箱:
PHP PHP
if(strpos($qqmail, '@qq.com'))
如果是 QQ 郵箱的話,則調用 QQ 頭像:
PHP PHP
$avatar_source = 'q.qlogo.cn';
$img = 'g?b=qq&nk=' . preg_replace('/@qq.com/', '', $qqmail) . '&s=100';
否則仍然調用 Gravatar 頭像:
PHP PHP
$avatar_source = 'cn.gravatar.com';
$img = 'avatar/$1?s=$2';
這樣這個問題就愉快地解決了。
完整代碼如下:
PHP PHP
// 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]+)&.*/', '<img class="avatar avatar-$2" src="' . $protocol . '://' . $avatar_source . '/' . $img . '" width="$2" height="$2" />', $avatar);
return $avatar;
}
add_filter('get_avatar', 'get_avatar_javst');
評論
(11)看看,试试
怎么使用呢?
QQ获取头像的接口改了,WordPress获取头像的方式也变了,这篇教程作废
www.zhulouren.com 测试
你在测试啥
不能拉去qq用户的名称吗?
API可能已经失效了,毕竟是几年前写的
有机会能,交换一个友链吗?我的网站还在建设
您的网站是做什么内容的
一知半解啦
请问 你这个 如何 做到 呢 好好玩