Tips for caching Gravatar headers in WordPress to a local machine and Related Optimization. _ PHP Tutorial

Source: Internet
Author: User
In WordPress, the Gravatar Avatar is cached locally and related optimization skills ,. In WordPress, the Gravatar Avatar is cached locally and related optimization techniques. the purpose of the Gravatar global Avatar cache is to speed up website opening, because the Gravatar official website server caches Gravatar portraits locally in WordPress and related optimization techniques,

The purpose of caching Gravatar global profile pictures is to speed up website opening. because the Gravatar official website server is abroad and the great GFW is added, the opening speed in China is often slow. The method is from willin, but it seems that his website cannot be opened --

Cache Gravatar global Avatar locally

The cache method is as follows:

1. create a cache Directory
Create a folder named avatar in the WordPress root directory and set the ACL for this folder to 0755 (if 0755 is not available, try 0777 ).

2. set the default avatar
Prepare a default avatar (32*32) which is named "default.jpg" and placed in the avatar folder.

3. add cache code
Copy the following code to the topic's functions. php file.

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 = 1209600; // set the value to 14 days. unit: Second if (! Is_file ($ e) | (time ()-filemtime ($ e)> $ t) {// copy (htmlspecialchars_decode ($ g), $ e);} else $ avatar = strtr ($ avatar, array ($ g => $ w. '/avatar/'.f.'.jpg'); if (filesize ($ e) <500) copy ($ w. '/avatar/default.jpg', $ e); return $ avatar;} add_filter ('Get _ avatar ', 'My _ avatar ');


Determine whether the user has a Gravatar profile picture by Email
Most of the time, we also need to know whether the user has set Gravatar. for example, if the user has not set the Gravatar profile, the local default profile will be displayed directly, or the Gravatar profile will be set for users who have not set Gravatar, do not disturb users who have already set their profile pictures. The code shared today can be verified by Email address if the user has a Gravatar profile picture:

function validate_gravatar($email) { $hash = md5(strtolower(trim($email))); $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404'; $headers = @get_headers($uri); if (!preg_match("|200|", $headers[0])) { $has_valid_avatar = FALSE; } else { $has_valid_avatar = TRUE; } return $has_valid_avatar;}

Gravatar global Avatar cache optimization

The optimized code can avoid the resource waste and repeated connection problems caused by repeated cache of multiple files by default when the Gravatar Avatar is not set. The optimized code is as follows:

Function my_avatar ($ email, $ size = '32', $ default = '', $ alt ='') {$ f = md5 (strtolower ($ email )); // the following code caches the avatar to $ a = get_bloginfo ('Template _ url') in the wp-content directory '). '/avatar /'. $ f. $ size. '.png '; $ e = get_template_directory (). '/avatar /'. $ f. $ size. '.png '; $ d = get_template_directory (). '/avatar /'. $ f. '-d.png '; // to cache the avatar to the current topic directory, change the 3-5 lines of code to: // $ a = get_bloginfo ('Template _ url '). '/av Atar /'. $ f. $ size. '.png '; // $ e = get_template_directory (). '/avatar /'. $ f. $ size. '.png '; // $ d = get_template_directory (). '/avatar /'. $ f. '-d.png '; if ($ default = '') $ default = get_bloginfo ('wpurl '). 'Avatar/default.jpg '; $ t = 2592000; // The cache is valid for 30 days. unit: Second if (! Is_file ($ e) | (time ()-filemtime ($ e)> $ t) {if (! Is_file ($ d) | (time ()-filemtime ($ d)> $ t) {// verify whether there is an avatar $ uri =' http://www.gravatar.com/avatar/ '. $ F .'? D = 404 '; $ headers = @ get_headers ($ uri); if (! Preg_match ("| 200 |", $ headers [0]) {// no avatar, create a blank file as the tag $ handle = fopen ($ d, 'W'); fclose ($ handle); $ a = $ default;} else {// update $ r = get_option ('Avatar _ rating') if an avatar exists '); $ g =' http://www.gravatar.com/avatar/ '. $ F .'? S = '. $ size. '& r = '. $ r; copy ($ g, $ e) ;}} else {a = $ default ;}$ avatar = ""; return apply_filters ('My _ avatar ', $ avatar, $ email, $ size, $ default, $ alt );}

To use this method, replace the get_avatar function with my_avatar in all the files of the topic.
And if it is

get_avatar( $comment,

The format must be changed

my_avatar( $comment->comment_author_email

Because the my_avatar function can only retrieve user portraits by Email, you need to change the first parameter to the email address in the preceding case.

Articles you may be interested in:
  • How to filter links and SQL statements in WordPress
  • How to register and call menus in WordPress
  • PHP code example for updating pseudo-static rules in WordPress
  • Use the Fix Rss Feeds plug-in to Fix WordPress Feed display errors

The purpose of caching Gravatar global profile pictures is to speed up website opening, because the servers on the Gravatar official website are in...

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.