More and more websites now support Gravatar portraits, and more netizens have set up personalized Gravatar portraits. wordpress also uses Gravatar portraits by default, but Gravatar is always in progress, when a page needs to display many Gravatar portraits, it will inevitably affect the page loading speed. To improve user experience, we can cache the profile picture files to the local server.
First, create a folder under the root directory of your blog to store the cached Gravatar profile file, such as "avatar". Set the permission to other files in the same directory. Then, if you modify the functions. Php file or the functions file that comes with the topic, you can modify it on the functions file of the topic, add the following code at the end, and set the Avatar timeout days. Then, refresh the page to see if the avatar is properly displayed. Then, go to the background to see if cache files are created under the avatar directory.
<? Php
Function my_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 = 2592000 ;//? Set to 30 days ,? Why? Seconds
If (! Is_file ($ e) | (time ()-filemtime ($ e)> $ t ){//?? Does the image not exist or the file is too large? Updated in 30 days
Copy (htmlspecialchars_decode ($ g), $ e );
} Else $ avatar = strtr ($ avatar, array ($ g => $ w. '/avatar/'.w.f.'.jpg '));
If (filesize ($ e) <500) copy ($ w. '/avatar/default.jpg', $ e );
Return $ avatar;
}
Add_filter ('Get _ avatar ', 'My _ avatar ');
?>
In addition to this method, you can also use plug-ins to implement it. Now we have sorted out several plug-ins
Avatar cache plugin:
GravatarLocalCache
FV Gravatar Cache
WP Gravatar Mini Cache
Gravatar Cache
Hacklog Gravatar Cache
The above Avatar cache plug-in provides similar functions. It downloads the Gravatar avatar to a local server and then reads the cached image to increase the loading speed. After the plug-in is enabled, it may be slow to open the page with an avatar for the first time, because the cached image is being downloaded, and the speed will be significantly improved.