Enable Gravatar Avatar Cache to Resolve Slow WordPress Loading

Publish: 2014-11-15 | Modify: 2014-11-24

A few months ago, Google was blocked by GFW, and WordPress loaded fonts from Google, which caused abnormal slowness. If the problem has not been solved, please see here. And recently, I found that the speed of my blog has become slow again. I checked the reason using F12 and found that the Gravatar avatar was blocked again. So I turned on Gravatar avatar cache to improve the speed of WordPress.

Gravatar

Gravatar avatar cannot be loaded.

Method 1: Duoshuo Cache (not recommended)

Add the following code to the functions.php file in the theme directory. The purpose of the code is to replace all Gravatar avatar resources with Duoshuo's Gravatar avatar mirror server. This method is relatively simple.

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

Only replace the cache server with V7V3. It is not known whether V7V3 can handle it if more people use it. Add the code to the functions.php file in the 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 complicated than the previous two, but using local cache may be more stable, and there is no need to worry about being blocked by service providers and not being able to load.

  1. Create a folder named "avatar" in the root directory of the website. (Change the permission 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 put it in the "avatar" folder.

  3. Add the following code to the functions.php file of the currently used 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 in seconds
if ( !is_file($e) || (time() - filemtime($e)) > $t ) { // If the avatar exceeds the set time or does not exist, update it
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 comment plugin, so there is no need to worry about the problem of loading Gravatar avatars on the front end (the avatars will be automatically cached to the Duoshuo server after using the Duoshuo plugin). However, it does not work on the backend, so I use Method 3 to solve this problem. Finally, thanks to V7V3 for providing the method. For more methods, please see: Gravatar avatar multiple acceleration solutions, improve WordPress speed. If you have any questions, you can directly add my QQ: 337003006.


Comments