子比主题V8.1适配Gravatar头像[已实装]

前言

众所周知,子比主题并不适配Gravatar头像,也去询问过作者,给的答复是暂不考虑,所以只能自己动手了。

图片[1] - 子比主题V8.1适配Gravatar头像[已实装] - 云晓晨 KaiQi.Wang

参考文献

参考登山亦有道博客的文章《子比主题使用Cravatar代替原有头像

修改教程

1. 修改zib-theme.php 文件

文件位置 /wp-content/themes/zibll/inc/functions/zib-theme.php

搜索 //文章缩略图 后,找到 zib_get_data_avatar 这个函数,更改此函数并添加新函数。

function zib_get_data_avatar($user_id = '', $size = '', $alt = '')
{
    $args = array(
        'size'   => $size,
        'height' => $size,
        'width'  => $size,
        'alt'    => $alt,
    );

	$avatar = zib_get_avatar(null, $user_id, $args);

    if (zib_is_lazy('lazy_avatar')) {
        $avatar = str_replace(' src=', ' src="' . zib_default_avatar() . '" data-src=', $avatar);
        $avatar = str_replace(' class="', ' class="lazyload ', $avatar);
    }
    return $avatar;
}

//刷新头像缓存
add_action('user_save_custom_avatar', function ($user_id) {
    wp_cache_delete($user_id, 'user_avatar');
    zib_get_data_avatar($user_id);
}, 10);

add_filter('pre_get_avatar', 'zib_get_avatar', 10, 3);


function zib_get_avatar($avatar, $id_or_email, $args) {
    $user_id = zib_get_user_id($id_or_email);
    
    // 已登录用户:使用自定义头像逻辑
    if ($user_id != 0) {
        $custom_avatar = zib_get_user_meta($user_id, 'custom_avatar', true);
        $alt = get_the_author_meta('nickname', $user_id) . '的头像' . zib_get_delimiter_blog_name();
        $avatar = $custom_avatar ? $custom_avatar : zib_default_avatar();

        // 优化头像地址
        $avatar = str_replace('tb.himg.baidu.com', 'himg.bdimg.com', $avatar);
        $avatar = preg_replace("/^(https:|http:)/", "", $avatar);

        $args['size'] = esc_attr($args['size']);
        return '<img alt="' . esc_attr($alt) . '" src="' . esc_url($avatar) . '" class="avatar' . ($args['size'] ? ' avatar-' . $args['size'] : '') . ' avatar-id-' . $user_id . '"' . ($args['size'] ? ' height="' . $args['size'] . '" width="' . $args['size'] . '"' : '') . '>';
    }

    // 未登录用户/匿名评论者:传递原始$id_or_email(可能是评论对象或邮箱)
    else {
        // 调用头像函数时传入原始的$id_or_email,而非$user_id(0)
        return zib_get_comment_gavatar($avatar, $id_or_email, $args);
    }
}

//继续封装评论区$avatar头像数据
// 评论区头像处理(未登录用户)
function zib_get_data_comment_gavatar($id_or_email = '', $size = '', $alt = '') {
    $args = array(
        'size'   => $size,
        'height' => $size,
        'width'  => $size,
        'alt'    => $alt,
    );

    $avatar = zib_get_comment_gavatar(null, $id_or_email, $args);

    // 统一使用zib_is_lazy判断懒加载(与其他头像函数保持一致)
    if (zib_is_lazy('lazy_avatar')) {
        $avatar = str_replace(' src=', ' src="' . zib_default_avatar() . '" data-src=', $avatar);
    }
    return $avatar;
}

// 单独获取Cravatar头像(处理未登录/匿名用户)
function zib_get_comment_gavatar($avatar, $id_or_email, $args) {
    $alt = '访客头像' . _get_delimiter() . get_bloginfo('name');
    $email = '';

    // 从$id_or_email中提取邮箱(可能是评论对象或邮箱字符串)
    if (is_object($id_or_email) && isset($id_or_email->comment_author_email)) {
        // 评论对象:直接获取邮箱
        $email = $id_or_email->comment_author_email;
        $alt = get_comment_author($id_or_email) . '的头像' . _get_delimiter() . get_bloginfo('name');
    } elseif (is_string($id_or_email) && is_email($id_or_email)) {
        // 邮箱字符串:直接使用
        $email = $id_or_email;
    }

    // 生成Cravatar头像URL
    if (empty($avatar) || !$email) {
        if (empty($email)) {
            // 无邮箱时使用默认头像
            $avatar_url = zib_default_avatar();
        } else {
            // 有邮箱时生成Cravatar链接
            $hash = md5(strtolower(trim($email)));
            $avatar_url = 'https://weavatar.com/avatar/' . $hash . '?s=768&d=https://blog.kaiqi.wang/wp-content/themes/zibll/img/avatar-default.png';
        }
    } else {
        // 优化已有头像地址(去除协议前缀)
        $avatar_url = preg_replace("/^(https:|http:)/", "", $avatar);
    }

    // 生成img标签(统一添加懒加载类)
    return '<img alt="' . esc_attr($alt) . '" src="' . esc_url($avatar_url) . '" class="lazyload avatar avatar-' . esc_attr($args['size']) . '" height="' . esc_attr($args['size']) . '" width="' . esc_attr($args['size']) . '">';
}




function zib_get_comment_avatar_admin($avatar, $id_or_email, $args) {
    $email = '';
    $alt = '头像';
    $user_id = 0; // 初始化用户ID为0(未登录状态)

    if (is_admin()) {
        // 获取用户ID、邮箱和alt文本
        if (is_object($id_or_email)) {
            if ($id_or_email instanceof WP_Comment) {
                // 评论对象处理
                $user_id = $id_or_email->user_id; // 获取评论者用户ID
                $email = strtolower(trim($id_or_email->comment_author_email));
                $alt = get_comment_author($id_or_email) . '的头像' . zib_get_delimiter_blog_name();
            } elseif ($id_or_email instanceof WP_User) {
                // 用户对象处理
                $user_id = $id_or_email->ID; // 获取用户ID
                $email = strtolower(trim($id_or_email->user_email));
                $alt = $id_or_email->display_name . '的头像' . zib_get_delimiter_blog_name();
            }
        } elseif (is_numeric($id_or_email)) {
            // 用户ID处理
            $user_id = intval($id_or_email);
            $user = get_user_by('id', $user_id);
            if ($user) {
                $email = strtolower(trim($user->user_email));
                $alt = $user->display_name . '的头像' . zib_get_delimiter_blog_name();
            }
        } elseif (is_string($id_or_email)) {
            // 邮箱字符串处理(通常为未登录用户)
            $email = strtolower(trim($id_or_email));
        }
        // 已登录用户:使用自定义头像逻辑
        if ($user_id != 0) {
            $custom_avatar = zib_get_user_meta($user_id, 'custom_avatar', true);
            $alt = get_the_author_meta('nickname', $user_id) . '的头像' . zib_get_delimiter_blog_name();
            $avatar = $custom_avatar ? $custom_avatar : zib_default_avatar();

            // 优化头像地址
            $avatar = str_replace('tb.himg.baidu.com', 'himg.bdimg.com', $avatar);
            $avatar = preg_replace("/^(https:|http:)/", "", $avatar);

            $size = isset($args['size']) ? esc_attr($args['size']) : 45;
            return '<img alt="' . esc_attr($alt) . '" src="' . esc_url($avatar) . '" class="avatar' . ($size ? ' avatar-' . $size : '') . ' avatar-id-' . $user_id . '"' . ($size ? ' style="border-radius: 5px;" height="' . $size . '" width="' . $size . '"' : '') . '>';
        }

        // 未登录用户/匿名评论者:使用原有头像逻辑
        else {
            $size = isset($args['size']) && $args['size'] ? intval($args['size']) : 45;

            if (empty($email)) {
                $avatar = zib_default_avatar();
            } else {
                $hash = md5(strtolower(trim($email)));
                echo '<script>console.log("邮箱哈希值' . esc_js($hash) . '");</script>';
                $avatar = 'https://weavatar.com/avatar/' . $hash . '?s=' . $size . '&d=https://blog.kaiqi.wang/wp-content/themes/zibll/img/avatar-default.png';
            }

            return '<img alt="' . esc_attr($alt) . '" src=" '. esc_url($avatar) . ' " data-src="' . esc_url($avatar) . '" class="avatar avatar-' . esc_attr($size) . ' lazyload" style="border-radius: 5px;" height="' . esc_attr($size) . '" width="' . esc_attr($size) . '">';

        }
    }

    // 非管理员页面返回原始头像
    return $avatar;
}
add_filter('get_avatar', 'zib_get_comment_avatar_admin', 10, 3);

2. 修改 zib-user.php 文件

文件位置 /wp-content/themes/zibll/inc/functions/zib-user.php

搜索 zib_get_avatar_box 这个函数后,并再其下方增加新函数。

//评论区专用获取用户头像
function zib_get_comment_avatar_box($user_id = 0, $comment = null, $class = 'avatar-img', $link = true, $vip = true)
{
    // 1. 区分登录用户和匿名评论者,获取头像
    if ($user_id) {
        // 登录用户:调用用户头像函数
        $avatar_img = zib_get_data_avatar($user_id);
    } elseif ($comment instanceof WP_Comment) {
        // 匿名评论者:调用评论者头像函数(需提前定义zib_get_data_comment_gavatar)
        $avatar_img = zib_get_data_comment_gavatar($comment);
    } else {
        // 无用户ID且无评论对象:使用默认头像
        $avatar_img = '<img src="' . zib_default_avatar() . '" class="avatar" alt="默认头像">';
    }

    // 2. VIP徽章仅对登录用户显示
    $vip_icon = $vip && $user_id ? zib_get_avatar_badge($user_id) : '';

    // 3. 组装头像容器HTML
    $html = '<span class="' . esc_attr($class) . '">' . $avatar_img . $vip_icon . '</span>';

    // 4. 链接仅对登录用户生效
    if ($link && $user_id) {
        $user_url = zib_get_user_home_url($user_id);
        $html = '<a href="' . esc_url($user_url) . '">' . $html . '</a>';
    }

    return $html;
}

3. 修改 zib-comments-list.php 文件

文件位置 /wp-content/themes/zibll/inc/functions/zib-comments-list.php

  1. 找到 zib_get_comment_header 函数,其中对 author_avatar 变量赋值语句改成下方代码。
$author_avatar = zib_get_comment_avatar_box($user_id, $comment, 'avatar-img comt-avatar');

2. 找到 zib_widget_comments 函数,其中对 avatar 变量赋值语句改成下方代码。

$avatar = zib_get_comment_avatar_box($user_id, $comment, 'avatar-img comt-avatar');
  • 修改了评论区、侧边评论、WP后台评论这三个地方,其中的部分代码修改为自己的链接。
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
访客头像 - 云晓晨 KaiQi.Wang
欢迎您留下宝贵的见解!
提交
访客头像 - 云晓晨 KaiQi.Wang

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容