Detailed analysis of the avatar cache in WordPress and the caching Update method in the proxy _php tips

Source: Internet
Author: User
Tags nginx server wordpress update

WordPress comments in the avatar is the use of Gravatar Avatar Service (Gravatar official registered Address: http://en.gravatar.com), the user's cache head is generally fixed, so we can cache the avatar to the local to improve the speed of our site access.
Avatar cache for my wordpress avatar directory:

WordPress Avatar Cache Function Setting method
the first is to create a folder avatar under the root directory, permission 755. Again in the inside put a default head picture (default.jpg), no head of the children's shoes will be used by default. The code is as follows:

function My_avatar ($email, $size = ' n ', $default = ', $alt = ') {$f = MD5 (Strtolower ($email)); $a = Wp_content_url. '/avatar/'. $f. $size.
 '. png '; $e = Wp_content_dir. '/avatar/'. $f. $size.
 '. png '; $d = Wp_content_dir. '/avatar/'. $f.

 '-d.png '; if ($default = = ") $default = ' http://www.wpnoob.cn/avatar/default.jpg '; Size needs to be changed to your own site comments default Avatar $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 = " ';
Return apply_filters (' My_avatar ', $avatar, $email, $size, $default, $alt);

 }

Add the above code to your subject's functions.php file.
Replace the Get_avatar function that gets the avatar address with the My_avatar. With one exception, the functions.php comment list function:

Get_avatar ($comment

Need to change to:

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.

Get_avatar Function Introduction:
The above method is simple and convenient. But there's one more step to pay attention to. It is to be confirmed that the Get_avatar function is used where the avatar is called. Generally, only the old theme are not. If you don't, you can make amends.

For example, read:

<?php
 Echo Get_avatar ($comment->comment_author_email, $size = ' i ', $default = Get_bloginfo (' Wpurl '). '/avatar/default.jpg '); 
? >

How to update the Css/js file cache in agent (squid)
in WordPress add css or JS files, we generally use these four functions to achieve:

    • Wp_enqueue_script ()
    • Wp_enqueue_style ()
    • Wp_register_script ()
    • Wp_register_style ()

You can define the version number of Css/js in the function so that we can know the cache of the browser when we update the Css/js file, the default version number is the version number of WordPress. The version number is linked to the CSS/JS full path, typically after the version number changes, the full URL of the Css/js loaded style is changed, and the browser discovers that the URL change will request Css/js file again, so that the latest Css/js file can be reached.

But many agent software (such as squid) does not support "?" In the form of cache, we use the reverse proxy to cache our site, especially after squid3.0, has begun to not take "?" The URL of the number is cached. So if we want to use Squid's caching function, we have to remove the "?" and update the Squid agent's cache only by modifying the filename.

Below we will introduce in WordPress through the control of version number to modify the JS/CSS filename so that the agent software to achieve caching purposes:
1, in our subject code functions.php file to add the following code:

/** 
 * description:wordpress update css/js file cache in Agent (squid)
 * author:wordpress Tutorial Network
 * Author uri:http:// www.wpnoob.cn/
/function ds_filename_based_cache_busting ($SRC) {
 //admin's background Css/js file does not need to process
 if (is_ Admin ()) return
 $src;
 Adding the version number to the file name has the "." Number to differentiate return
 preg_replace (
 '/\. JS|CSS) \?ver= (. +) $/',
 '. $2.$1 ',
 $src
 );
}
Add_filter (' script_loader_src ', ' ds_filename_based_cache_busting ');
Add_filter (' style_loader_src ', ' ds_filename_based_cache_busting ');

If you are using the Apache server, add to the. htaccess file in your root directory:

<ifmodule mod_rewrite.c>
   rewriteengine on
   rewritebase/
 
   rewritecond%{request_filename}!-f
   Rewritecond%{request_filename}!-d
   Rewriterule ^ (. +) \. (.+)\. (JS|CSS) $ $1.$3 [L]
</IfModule>


If you are Nginx server configuration is as follows:

Location ~ ^ (. +) \. (.+)\. (JS|CSS) $ {
  alias $1.$3;
}

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.