评论随机头像

因为不知道出了什么问题读取不了QQ头像了,所以在原有的代码的基础上增加了随机头像

更改的位置应该在functions.php文件的1850行左右,将QQ Avatar 获取和判断 QQ 号合法性的内容更换以下代码即可

具体的url自行修改,注释也挺全的,实测也能运行

// 获取评论者头像
function get_avatar_by_qqnumber($avatar) {
    global $comment;

    // 确保评论存在
    if (!isset($comment) || !isset($comment->comment_ID)) {
        return $avatar;
    }

    // 获取评论者的用户 ID
    $user_id = get_comment_user_id_by_id($comment->comment_ID);

    // 检查评论发送者是否是已登录用户
    if ($user_id != 0) {
        // 使用唯一的 URL 作为头像
        $unique_avatar_url = '头像url'; // 替换为实际的唯一 URL

        preg_match_all('/width=\'(.*?)\'/', $avatar, $preg_res);
        $size = !empty($preg_res[1]) ? $preg_res[1][0] : 64; // 默认头像大小

        return "<img src='" . esc_url($unique_avatar_url) . "' class='avatar avatar-" . esc_attr($size) . " photo' width='" . esc_attr($size) . "' height='" . esc_attr($size) . "'>";
    }

    // 获取 QQ 号码
    $qqnumber = get_comment_meta($comment->comment_ID, 'qq_number', true);

    // 如果 QQ 号码存在且合法,尝试返回 QQ 头像
    if (!empty($qqnumber) && check_qqnumber($qqnumber)) {
        preg_match_all('/width=\'(.*?)\'/', $avatar, $preg_res);
        $size = !empty($preg_res[1]) ? $preg_res[1][0] : 64; // 默认头像大小

        return "<img src='https://q1.qlogo.cn/g?b=qq&s=640&nk=" . esc_attr($qqnumber) . "' class='avatar avatar-" . esc_attr($size) . " photo' width='" . esc_attr($size) . "' height='" . esc_attr($size) . "'>";
    }

    // QQ 头像获取失败,返回基于评论者 ID 的头像
    $random_avatar_url = '头像api/url'; // 替换为真实的 API URL

    preg_match_all('/width=\'(.*?)\'/', $avatar, $preg_res);
    $size = !empty($preg_res[1]) ? $preg_res[1][0] : 64; // 默认头像大小

    // 返回随机头像
    return "<img src='" . esc_url($random_avatar_url) . "?user_id=" . esc_attr($user_id) . "' class='avatar avatar-" . esc_attr($size) . " photo' width='" . esc_attr($size) . "' height='" . esc_attr($size) . "'>";
}

add_filter('get_avatar', 'get_avatar_by_qqnumber');

// 判断 QQ 号合法性
if (!function_exists('check_qqnumber')) {
    function check_qqnumber($qqnumber) {
        return preg_match("/^[1-9][0-9]{4,10}$/", $qqnumber) === 1;
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇
Document