
在国内访问 Gravatar 的头像服务器速度慢甚至无法显示,这是 WordPress 用户普遍遇到的问题。本文将教你如何使用反代 CDN 替换 Gravatar,让评论头像重新显示,并附带代码讲解,适合 Astra 等所有主题使用。
🧩 问题背景
WordPress 默认使用 gravatar.com 加载头像,但这个域名在国内常年访问困难,因此评论区经常看不到访客头像。
🛠️ 解决方案:使用国内反代服务
我们通过 WordPress 的 get_avatar_url 过滤器,把默认的 Gravatar 头像链接替换为国内可访问的 CDN。
✅ 推荐使用的国内 Gravatar 镜像地址(任选一个):
| 镜像来源 | 地址示例 |
|---|---|
| Cravatar | https://cravatar.cn/avatar/ |
| Geekzu CDN | https://sdn.geekzu.org/avatar/ |
| weavatar | https://weavatar.com/avatar/ (稳定镜像) |
建议优先使用 Cravatar,更快、更稳定。
📦 实现代码
将以下代码加入你主题或子主题的 functions.php 中:
add_filter('get_avatar_url', 'replace_gravatar_domain', 10, 3);
function replace_gravatar_domain($url, $id_or_email, $args) {
// 改用 WeAvatar 稳定镜像
$cdn = 'https://weavatar.com/avatar/';
$url = preg_replace('#https?://([^\s]*\.)?gravatar\.com/avatar/#i', $cdn, $url);
return $url;
}
🔍 正则替换说明
这一行代码:
return preg_replace('#https?://([a-z0-9]+\.)*gravatar\.com/avatar/#', $cdn, $url);
可以匹配各种头像地址形式,包括:
https://secure.gravatar.com/avatar/http://gravatar.com/avatar/https://0.gravatar.com/avatar/https://cn.gravatar.com/avatar/
并统一替换为你指定的 CDN 地址。
🚫 遇到头像缓存不更新?
Gravatar 的头像是根据邮箱 MD5 哈希生成的链接,服务器会缓存图片。你可能在 Gravatar 官网 更新头像后,还看的是旧图。
🔁 解决方法:
给头像链接加时间戳或版本参数:
$cdn = 'https://cravatar.cn/avatar/';
$timestamp = time();
return preg_replace('#https?://([a-z0-9]+\.)*gravatar\.com/avatar/#', $cdn, $url) . '&v=' . $timestamp;
⚠️ 注意:这样每次都强制刷新头像,可能增加加载时间,建议只在调试时使用。
🎨 延伸:如何隐藏 Astra 主题中的“作者”信息
Astra 默认在文章底部显示作者信息,如果你不想让它出现,可加入以下代码隐藏它:
add_action( 'wp', 'remove_astra_post_author' );
function remove_astra_post_author() {
if ( ! is_single() ) {
remove_action( 'astra_entry_header', 'astra_post_author', 10 );
remove_action( 'astra_post_footer', 'astra_post_author', 10 );
}
}
💡 其它建议
- 子主题编辑方式推荐:使用 FTP 或 SFTP 修改
functions.php文件,避免后台编辑器保存不了。 - 头像不显示请检查:
- 清理 WordPress 缓存插件
- 浏览器缓存
- 检查头像 URL 是否已替换
✅ 最终效果
头像将从如下地址加载,国内访问无压力:
https://cravatar.cn/avatar/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
** 小技巧:WordPress 的隐藏设置页面强制修改管理员邮箱,完全绕过发邮件验证的步骤。
1.在浏览器地址栏中,将当前的 WordPress 后台网址修改为 你的域名/wp-admin/options.php,然后按回车进入。
2.2.查找 admin_email 字段:使用浏览器搜索。
进入页面后,你会看到一个包含所有网站数据的长列表。按下键盘的 Ctrl + F(Mac 是 Cmd + F),在页面中搜索 admin_email。
3.3.填入新邮箱并保存:立即生效。
找到 admin_email 后,将旁边文本框里的旧邮箱直接删除,输入你想要的新邮箱。然后一直滚动到页面最底部,点击 保存更改 (Save Changes)。
Everything is just so professional here. From the layout to the betting options, it is all high quality. I always use 888winbetlogin for my daily plays.
I’ve been using this app for a few weeks now and the interface is super smooth. Definitely one of the best experiences I’ve had lately. Check out 8999bdapp for more details.
I’ve tried many platforms but this one stands out for its transparency. Everything is clear and the wins feel earned. Love phjilibet777.
Love the VIP treatment here. The rewards are actually generous and the support team is very helpful. Five stars for takbatvip.
The community here is great and the odds are very competitive. I’ve had a few big wins already this week. Check out idmamajitu for a solid gaming experience.
Top notch graphics and really engaging gameplay. It is honestly one of the best gaming experiences I have had in a long time. Big thanks to ck888game
Really impressed with the odds and the overall vibe of the site. It feels very secure and fair for the players. Give br79bet42 a try if you are looking for something fresh.
Mwingame huh? Never heard of it, but the name is kinda catchy! Guess I’ll give it a whirl. Fingers crossed! You can check it here: mwingame
PKRVIPGAME sounds intriguing! Are the games any good? I’m always on the hunt for a new place to play. Let’s explore! pkrvipgame
Jljlbb? Heard some good stuff about it! Gotta check it out when I have some free time. Maybe I’ll even win big! Learn more here: jljlbb