Detailed description of WordPress in the avatar cache and Proxy cache Update method, wordpress avatar _php Tutorial

Source: Internet
Author: User
Tags nginx server wordpress update

Detailed description of WordPress in the avatar cache and Proxy cache Update method, wordpress Avatar


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

WordPress Avatar Cache Function Setting method
The first is to create a folder under the root directory avatar, permissions 755. Put a default avatar (default.jpg) in it, the children's shoes with no avatar will be used by default. The code is as follows:

function My_avatar ($email, $size = ' + ', $default = ', $alt = ') {$f = MD5 (Strtolower ($email)); $a = Wp_content_ur L. '/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 '; The size needs to be changed to your own website comments default Avatar $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);} 

Add the above code to your subject's functions.php file.
Replace the Get_avatar function that gets the avatar address with My_avatar. There is an exception to 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 be transferred via email to the user's avatar, so the above situation, you need to change the first parameter to an email address.

Get_avatar Function Description:
It is simple and convenient to use the above method. But there is one more step to be aware of. You have to make sure that the Get_avatar function is used where the avatar is called. Generally, only the old theme is not. If not, you can do it.

If instead:

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

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

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

In the function you can define the version number of the Css/js so that we can clear the browser cache when updating 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, usually after the change of the version number, css/js the full URL of the loaded style will also change, the browser found that the URL changes will re-request the Css/js file, so that it can be loaded into the latest Css/js file.

But many agent software (such as squid) does not support "?" In the form of the cache, we use the reverse proxy to cache our site, especially after squid3.0, has started not to take "?" The URL of the number is cached. So if we want to use SQUID cache function must remove "?", update squid agent cache can only be done by modifying the file name.

Below we will introduce in WordPress by the version number of the control to modify the Js/css file name so that the proxy software to achieve the purpose of caching:
1. Add the following code to our subject code functions.php file:

/**  * description:wordpress update css/js file cache in Agent (squid) * author:wordpress Tutorial Net * Author uri:http://www.wpnoob.cn/* * function ds_filename_based_cache_busting ($SRC) {//admin background Css/js file does not need to process if (Is_admin ()) return $SRC;//Add version number to filename "." To differentiate the 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 an Apache server, add it under the. htaccess file in your root directory:

 
  
   
     Rewriteengine on   rewritebase/    rewritecond%{request_filename}!-f   rewritecond%{request_filename}!-d   rewriterule ^ (. +) \. (.+)\. (JS|CSS) $ $1.$3 [L]
 
  


If you are the Nginx server configuration is as follows:

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

Articles you may be interested in:

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

http://www.bkjia.com/PHPjc/1105380.html www.bkjia.com true http://www.bkjia.com/PHPjc/1105380.html techarticle detailed description of WordPress in the avatar cache and the proxy cache Update method, wordpress avatar WordPress Comments in the avatar is the use of Gravatar's avatar Service (Gravatar official registered Address: HTTP ...

  • 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.