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!