Using the QQ Avatar Source for WordPress Comments with QQ Email

16,032 11

It must be said that many people in China are not accustomed to using email, and some do not even have Gravatar avatars. Accordingly, the theme has been optimized to some extent for the situation in China. If a QQ email address is used, the QQ avatar is called; otherwise, the Gravatar avatar continues to be called. This is convenient for both QQ users and Gravatar users. This method does not slow down webpage loading, so it can be said to be a relatively good method.

Image

First, we found the interface for obtaining a QQ avatar: https://q.qlogo.cn/g?b=qq&nk=52958812&s=100. However, we cannot directly obtain the commenter's QQ number, so we need to obtain the commenter's email address:

PHP
get_comment($parent_id)->comment_author_email);

With the email address, determine whether it is a QQ email address:

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

If it is a QQ email address, call the QQ avatar:

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

Otherwise, continue to call the Gravatar avatar:

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

Thus, this problem is happily solved.

The complete code is as follows:

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]+)&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');

Comments

(11)
  1. 果壳Sir
    MIUI Browser 10 Android 9MIX 3

    看看,试试

  2. 果壳Sir
    MIUI Browser 10 Android 9MIX 3

    怎么使用呢?

    1. Chrome 76 macOS 10.14.6Apple Macintosh

      QQ获取头像的接口改了,WordPress获取头像的方式也变了,这篇教程作废

  3. Sogou Explorer 1 Windows 7

    www.zhulouren.com 测试

  4. Edge 18 Windows 10

    不能拉去qq用户的名称吗?

  5. Edge 18 Windows 10

    有机会能,交换一个友链吗?我的网站还在建设

  6. QQBrowser 10 Windows 10

    一知半解啦

  7. QQBrowser 10 Windows 10

    请问 你这个 如何 做到 呢 好好玩

Leave a comment