Using Qiniu Cloud Storage to Accelerate Gravatar Avatars

Publish: 2015-10-25 | Modify: 2017-06-21

In the previous period of time, Gravatar avatars were blocked, causing many WordPress sites to load very slowly. If you haven't solved this problem yet, you can refer to this article: Gravatar头像多种加速方案,提升WordPress速度. However, recently cn.gravatar.com has been accessible again, but the speed is still slow if you load Gravatar avatars from the official website.

Therefore, we can consider building a Gravatar avatar cache server, for example, caching avatars to a local server. But personally, I don't think this is the best solution. Next, I'll share how to use Qiniu Cloud Storage to cache Gravatar avatars and achieve acceleration.

一、创建七牛云存储账号

First, it is necessary to create an account on Qiniu Cloud. After verification, you can use 10GB of space and 10GB of traffic for free, which is completely sufficient for small websites. Click here to register.

二、创建一个公开空间(bucket)

Click the "New Space" button in the upper left corner -> Fill in the space name -> Set the access control to public and the usage scenario to general -> Finally, click the submit button to create.

三、开始配置

After the space is created successfully, we need to configure it. First, click the "One-click Acceleration" button, then fill in the mirror source as "http://cn.gravatar.com", and keep the other settings as default. Finally, click the "Confirm Acceleration" button to save.

四、添加代码

Before adding the code, we need to get the free domain name provided by Qiniu. You can find it in the domain settings, copy and save the domain name you see.

Finally, add the following code to the "functions.php" file in your theme directory. You need to modify the domain name at the end of the code to the domain name you just obtained from Qiniu or your own bound domain name.

//Gravatar缓存头像
function xiaoz_get_avatar($avatar) {
    $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),
"your domain name obtained from Qiniu",$avatar);
    return $avatar;
}
add_filter( 'get_avatar', 'xiaoz_get_avatar', 10, 3 );
//缓存头像END

五、访问测试

Finally, find an article with comments and test whether the user avatars are loaded from the Qiniu domain. If you are using the Chrome browser, right-click the image and open it in a new tab to view it. If it is the domain name of Qiniu or your bound domain name, it means it has been successful.


Comments