Create a folder in the wordpress Directory: avatar. The folder permission is 755. If you do not have the folder permission, set it to 777. This is the path to the gravatar avatar cache.
Set default avatar
Create a default avatar, or copy it directly in the wordpress background settings, and name it "default.jpg", which is placed in the avatar folder. I can't open it. I will provide a picture I'm using.
Add cache code
Copy the following code to the template's functions. php file:
The code is as follows: |
Copy code |
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 = home_url (); // $ w = get_bloginfo ('URL '); $ E = preg_replace ('/wordpress //', '', ABSPATH). 'Avatar/'. $ f .'.jpg '; $ T = 604800; // Set 7 days, in seconds If (empty ($ default) $ default = $ w. '/avatar/default.jpg '; 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.'.jpg ')); If (filesize ($ e) <500) copy ($ default, $ e ); Return $ avatar; } Add_filter ('Get _ avatar ', 'My _ avatar '); |
After completing the above things, it will be OK, as set on this site.
Conclusion: in fact, it is quite good to cache Gravatar pictures locally. You can manage and maintain the items on your server. You can see that the big station has put the data on another server.