> For the complete documentation index, see [llms.txt](https://book.linh.eu.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.linh.eu.org/jian-zhan-ji-shu/xiu-gai-wordpress-he-typecho-zhan-dian-gravatar-tou-xiang-yuan.md).

# 修改WordPress和Typecho站点Gravatar头像源

由于Gravatar在我国大陆地区无法正常访问，目前有一些Gravatar镜像源，可以进行更换。我看到有很多人提出的方法要么是修改WordPress或Typecho的源代码，要么是修改主题源代码，但这些都会随着程序升级、主题升级或更换而失效。

因此，可以按照如下方法进行修改，可以避免如上问题。

### WordPress修改Gravatar头像源方法

安装Code Snippets插件，或者WP code插件，添加如下代码：

```
function custom_gravatar_cdn() {
    add_filter('get_avatar_url', function($url) 
        $url = preg_replace('/(?:http|https):\/\/\S+gravatar.com\/avatar\/(\S+)/', 'https://gravatar.webp.se/avatar/$1', $url);
        return $url;
    }, PHP_INT_MAX, 1);
}
add_action('init', 'custom_gravatar_cdn');
```

### Typecho修改Gravatar头像源方法

在config.inc.php中，加入以下代码：

```
define('TYPECHO_GRAVATAR_PREFIX', 'https://gravatar.webp.se/avatar');
```

注意：Typecho的主题如果将Gravatar地址的代码写死，则需要修改主题代码。

### Gravatar头像源推荐

速度比较快的Gravatar头像源有：

* <https://gravatar.webp.se/avatar>
* <https://gravatar.bluecdn.com/avatar>
* <https://weavatar.com/avatar>
* <https://cdn.sep.cc/avatar>
* <https://seccdn.libravatar.org/gravatarproxy>

Cravatar.cn镜像因为未知原因，导致很多人的头像都变成了“神秘人”，因此不推荐使用。
