How to cache Gravatar headers of different sizes in WordPress

Source: Internet
Author: User

Improved functions: supports different avatar sizes; supports automatic creation of cache directories.

The following is not much to say. Just add the code to function. php on your topic:

The code is as follows: Copy code

// Cache the Gravatar Avatar
Add_filter ('Get _ avatar ', 'fanly _ cache_avatar ');
Function fanly_cache_avatar ($ avatar ){
If (! Is_dir ('Avatar ') // determines whether a cache directory exists.
Mkdir ('Avatar ', 0755, true); // No, creates an avatar cache directory.
$ 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 = home_url ();
List ($ width, $ height, $ type, $ attr) = getimagesize ($ g); // Get the Avatar parameter
$ E = ABSPATH. 'Avatar/'. $ f .'-'.20.width.'.jpg ';
$ T = 604800; // Set 7 days, in seconds
If (empty ($ default) $ default = 'http: // I .leiue.com/avatar.php ';
If (! Is_file ($ e) | (time ()-filemtime ($ e)> $ t) {// When the avatar does not exist or the file has been updated for more than 7 days
Copy (htmlspecialchars_decode ($ g), $ e );
} Else {
$ Avatar = strtr ($ avatar, array ($ g => $ w. '/avatar/'.w.f.'-'.w.width.'.jpg '));
 }
If (filesize ($ e) <500) copy ($ default, $ e );
Return $ avatar;
}

During development, you can add a control function to the end of the code to enable or disable the Avatar cache function.

Of course, in the new version of WordPress, the get_avatar function already supports srcset to adapt to HD screens. Some bad code may cause problems in HD screens. Developers may need to develop and adapt to their preferences!

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.