Example of implementing Gravatar Avatar caching in WordPress

Source: Internet
Author: User
Tags php file

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.