代码放到主题下的functions.php文件最下面
function use_qq_avatar_for_missing_gravatar($avatar, $id_or_email, $size, $default, $alt) {
// 获取用户的邮箱
if (is_numeric($id_or_email)) {
$user = get_user_by(‘id’, $id_or_email);
$email = $user->user_email;
} elseif (is_object($id_or_email)) {
$email = $id_or_email->comment_author_email;
} else {
$email = $id_or_email;
}
// 检查邮箱是否为 QQ 邮箱
if (preg_match(‘/^[1-9][0-9]{4,10}@qq\.com$/i’, $email)) {
// 提取 QQ 号码
list($qq_number) = explode(‘@’, $email);
// 构建 QQ 头像 URL
$qq_avatar_url = “https://q1.qlogo.cn/g?b=qq&nk={$qq_number}&s=100”;
// 检查用户是否已有 Gravatar 头像
$gravatar_url = ‘https://www.gravatar.com/avatar/’ . md5(strtolower(trim($email))) . ‘?d=404’;
$response = wp_remote_head($gravatar_url);
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) == 404) {
// 没有 Gravatar 头像时使用 QQ 头像
$avatar = “<img src=”{$qq_avatar_url}” alt=”{$alt}” width=”{$size}” height=”{$size}” />”;
}
}
return $avatar;
}
add_filter(‘get_avatar’, ‘use_qq_avatar_for_missing_gravatar’, 10, 5);
© 版权声明
暂无评论...