Accelerate Gravatar Avatars for WordPress Using Qiniu Cloud Storage

gravatar accelerationqiniu cloud storagewordpress avatar cachegravatar mirrorwp functions.php
Published·Modified·

In the past, Gravatar avatars were blocked in China, causing many WordPress sites to load very slowly. If you haven't resolved this issue yet, you can refer to the article: Multiple Gravatar Acceleration Solutions to Improve WordPress Speed. Recently, cn.gravatar.com has resumed access, but loading avatars directly from the official website is still slow.

Therefore, we can consider building a self-hosted Gravatar cache server, such as caching avatars to a local server. However, I don't think this is the best solution. Next, I will share how to use Qiniu Cloud Storage to cache Gravatar avatars to achieve acceleration.

1. Create a Qiniu Cloud Storage Account

First, creating a Qiniu account is necessary. After authentication, you can use 10GB of storage space and 10GB of traffic for free, which is more than enough for small websites. Click here to register.

2. Create a Public Bucket

Click the "Create Bucket" button in the top left corner -> Enter a bucket name -> Set Access Control to Public and Usage Scenario to General -> Then submit to create.

Create Bucket

3. Start Configuration

After the bucket 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, keep other settings as default, and finally click the "Accelerate" button to save.

One-Click Acceleration

Acceleration Settings

4. Add Code

Before adding the code, we need to obtain the free domain name gifted by Qiniu. Switch to the Domain Settings page to retrieve it, then copy and save the domain name.

Domain Settings

Finally, add the following code to the functions.php file in your theme directory. In the code, you need to replace the domain at the end with the domain you obtained from Qiniu or your own bound domain.

// Gravatar Cache Avatar
function xiaoz_get_avatar($avatar) {
    $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),
"Your Qiniu Domain", $avatar);
    return $avatar;
}
add_filter( 'get_avatar', 'xiaoz_get_avatar', 10, 3 );
// End Cache Avatar

5. Access Test

Finally, find an article with comments to test if the user avatars are loaded from the Qiniu domain. If using the Chrome browser, right-click the image and open it in a new tab to check. If it shows the Qiniu domain or your bound domain, it means it was successful.

Test Result 1

Test Result 2