Enable Gravatar Avatar Caching to Improve WordPress Loading Speed

gravatar cachewordpress speed optimizationlocal avatar cachingduoshuo gravatar mirrorv7v3 gravatar cache
Published·Modified·

Several months ago, Google was blocked by the GFW, and WordPress loading fonts from Google caused significant slowness. If you haven't resolved this yet, see here for methods to load Google fonts on WordPress. Recently, I noticed my blog speed slowing down again. Using F12 to inspect the cause, I found that the global avatar service Gravatar was also blocked. Therefore, enabling Gravatar avatar caching to improve WordPress speed is urgent.

Gravatar

Gravatar avatars cannot be loaded.

Method 1: Duoshuo Cache (Not Recommended)

Add the following code to the functions.php file in your theme directory. This code replaces all Gravatar avatar resources with the Gravatar avatar mirror server provided by the domestic Duoshuo service. This is a relatively simple method.

function v7v3_get_avatar($avatar) {
    $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),
"gravatar.duoshuo.com",$avatar);
    return $avatar;
}
add_filter( 'get_avatar', 'v7v3_get_avatar', 10, 3 );

Method 2: V7V3 Cache

This method simply replaces the cache server with V7V3's. I am unsure if V7V3 can handle a high volume of users, but you can add the code to the functions.php file in your theme directory and save it.

function v7v3_get_avatar($avatar) {
    $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),
"cd.v7v3.com",$avatar);
    return $avatar;
}
add_filter( 'get_avatar', 'v7v3_get_avatar', 10, 3 );

Method 3: Local Cache

This method is slightly more complex than the previous two, but using local caching may be more stable and avoids the risk of being blocked by service providers.

  1. Create a folder named avatar in the root directory of your website. (Set permissions to 755 or 777). Some VPS users may need to change the user group to www.
  2. Create a default avatar image named default.jpg and place it in the avatar folder.
  3. Add the following code to the functions.php file of your current theme.
function v7v3_avatar($avatar) {
$tmp = strpos($avatar, 'http');
$g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
$tmp = strpos($g, 'avatar/') + 7;
$f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
$w = get_bloginfo('wpurl');
$e = ABSPATH .'avatar/'. $f .'.jpg';
$t = 1209600; // Set cache time to 14 days, unit is seconds
if ( !is_file($e) || (time() - filemtime($e)) > $t ) { // Update if avatar exceeds set time or does not exist
copy(htmlspecialchars_decode($g), $e);
} else $avatar = strtr($avatar, array($g => $w.'/avatar/'.$f.'.jpg'));
if (filesize($e) < 500) copy($w.'/avatar/default.jpg', $e);
return $avatar;
}
add_filter('get_avatar', 'v7v3_avatar');

Currently, my blog uses the Duoshuo social commenting plugin, so there is no issue with loading Gravatar avatars on the frontend (avatars are automatically cached to the Duoshuo server after using the plugin). However, this does not work for the backend, so I used Method 3 to solve this problem. Finally, thanks to V7V3 for providing this method. For more methods, please check: Gravatar Avatar Acceleration Solutions to Improve WordPress Speed. If you have any questions, you can add my QQ: 337003006.