WordPress in Gravatar avatar cache to local and related optimization tips, _php tutorial

Source: Internet
Author: User

WordPress in Gravatar avatar cache to local and related optimization techniques,


The purpose of the Gravatar global avatar cache is to speed up the opening of the website, because the Gravatar website's server is abroad, plus the great GFW, the domestic opening speed is often very slow. The method came 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 a cache directory
Create a folder named Avatar in the WordPress root directory, set the permissions for the folder to 0755 (if 0755 does not work, try 0777).

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

3. Add Cache Code
Copy the following code into the subject'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 = Abspa TH. ' Avatar/'. $f. " JPG '; $t = 1209600; Set 14-day, unit: Seconds if (!is_file ($e) | | (Time ()-Filemtime ($e)) > $t) {//When the image does not exist or the file is over 14 days to update copy (Htmlspecialchars_decode ($g), $e);} else $avatar = Strtr ($avat AR, Array ($g = $w. '/avatar/'. $f. jpg '), if (FileSize ($e) <) copy ($w. '/avatar/default.jpg ', $e); return $ Avatar;} Add_filter (' Get_avatar ', ' My_avatar ');


Determine if the user has a gravatar avatar via email
Many times, we also need to know that the user has not set Gravatar, such as if the user does not set Gravatar avatar directly displays the local default avatar, or to remind the user settings Gravatar not set Gravatar avatar, without disturbing users who have already set up the avatar. The code you share today can be verified with an 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 Avatar Cache optimization

Optimized code avoids the problem of resource wasting and duplicate connection when the user does not set Gravatar avatar, the default avatar is repeatedly cached into multiple files. The optimized code is as follows

function My_avatar ($email, $size = ' three ', $default = ', $alt = ') {$f = MD5 (Strtolower ($email));//The following code caches the avatar to Wp-co In the ntent 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 your avatar in the current theme directory, change the 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; The cache is valid for 30 days, here units: seconds if (!is_file ($e) | | (Time ()-Filemtime ($e)) > $t) {if (!is_file ($d) | | (Time ()-Filemtime ($d)) > $t) {//Verify if 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 new blank file as a marker $handle = fopen ($d, ' w '); Fclose ($handle);   $a = $default;    } else {//has an avatar and does not exist then update $r = get_option (' avatar_rating '); $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);}

Using this method, you need to replace the Get_avatar function with My_avatar in all the files in the subject.
And if it is

Get_avatar ($comment,

Format, you also need to change

My_avatar ($comment->comment_author_email

Because the My_avatar function can only be transferred via email to the user's avatar, so the above situation, you need to change the first parameter to an email address.

Articles you may be interested in:

    • A detailed explanation of how to filter links and filter SQL statements in WordPress
    • How to register menu and invoke menu in WordPress
    • Examples of PHP code used to update pseudo-static rules in WordPress
    • Fix wordpress feed display error with fix RSS feeds plugin

http://www.bkjia.com/PHPjc/1084596.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084596.html techarticle WordPress in Gravatar avatar cache to local and related optimization techniques, will gravatar global avatar cache is designed to speed up the opening of the website, because the Gravatar official website of the server 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.