WordPress gravatar Avatar Cache to local and related optimization techniques _php skills

Source: Internet
Author: User
Tags hash md5

The purpose of the Gravatar global avatar cache is to speed up the opening of the site, because the Gravatar official website of the server in foreign countries, plus the great GFW, the domestic opening speed is often very slow. The method comes from Willin, but it seems that his website has not been opened

Cache Gravatar Global Avatar to Local

The caching method is as follows:

1. Create cache Directory
In the WordPress root directory to establish a folder named Avatar, set the permissions of the folder is 0755 (if 0755 is not good, try 0777).

2. Set the default avatar
Prepare a default avatar with a size fit (32*32), named "Default.jpg", and put it in the Avatar folder.

3. Add Cache Code
Copy the following code to the functions.php file in the theme

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 14 days, Unit: SEC
if (!is_file ($e) | |  (Time ()-Filemtime ($e)) > $t) {//When the image does not exist or files exceed 14 days to update
copy (Htmlspecialchars_decode ($g), $e);
} else $avatar = STRTR ($avatar, Array ($g => $w. '/avatar/'. $f. jpg '));
if (FileSize ($e) <) copy ($w. '/avatar/default.jpg ', $e);
return $avatar;
}
Add_filter (' Get_avatar ', ' My_avatar ');


determine whether the user has gravatar avatar by email
many times, we also need to know if the user has not set the Gravatar, for example, if the user does not set the Gravatar avatar will directly display the local default avatar, or to remind users who do not set Gravatar settings Gravatar avatar, Without disturbing the user who has already set the avatar. Today's shared code can be verified by email address to see if the user has a Gravatar avatar:

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 General Avatar Cache optimization

The optimized code avoids the problem that the user does not set the Gravatar avatar, the default avatar is repeatedly cached as multiple files resulting in resource waste and repeated connections. The optimized code is as follows

function My_avatar ($email, $size = ' n ', $default = ', $alt = ') {$f = MD5 (Strtolower ($email)); The following code caches the avatar to the wp-content directory $a = get_bloginfo (' Template_url '). '/avatar/'. $f. $size.
 '. png '; $e = Get_template_directory (). '/avatar/'. $f. $size.
 '. png '; $d = Get_template_directory (). '/avatar/'. $f.
 '-d.png '; If you want to cache the avatar under the current topic directory, change 3-5 lines of code to://$a = get_bloginfo (' Template_url '). '/avatar/'. $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; Cache valid for 30 days, here unit: seconds if (!is_file ($e) | | (Time ()-Filemtime ($e)) > $t) {if!is_file ($d) | | (Time ()-Filemtime ($d)) > $t) {//verify Avatar $uri = ' http://www.gravatar.com/avatar/'. $f.
   ' d=404 ';
   $headers = @get_headers ($uri); if (!preg_match ("|200|", $headers [0])) {//no avatar, create a new blank file as a token $handle = FopeN ($d, ' w ');
    Fclose ($handle);
   $a = $default;
    else {//has avatar and does not exist update $r = get_option (' avatar_rating '); $g = ' http://www.gravatar.com/avatar/'. $f. ' s= '. $size. ' &r= '.
    $r;
   Copy ($g, $e);
  } else {$a = $default; } $avatar = " 

Use this method to replace the Get_avatar function with My_avatar in all files in the theme.
And if it is

Get_avatar ($comment,

format, you need to change

My_avatar ($comment->comment_author_email

Because the My_avatar function can only by email to the user avatar, so the above situation, need to change the first parameter to email address.

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.