Detailed description of the Avatar cache in WordPress and the cache update method in the proxy, wordpress Avatar _ PHP Tutorial

Source: Internet
Author: User
Tags wordpress version
The following describes how to update the Avatar cache and proxy in WordPress. For details about the Avatar cache in WordPress and the cache update method in the proxy, the avatar in the wordpress profile comment is the avatar service using Gravatar (Gravatar official registration address: http details of the Avatar cache in WordPress and the cache update method in the proxy, wordpress Avatar

Wordpress comments in the Avatar is the use of Gravatar Avatar service (Gravatar official registered address: http://en.gravatar.com), the user's cache Avatar is generally fixed, therefore, you can cache your profile picture locally to speed up website access.
The avatar cache in the my wordpress avatar Directory:

How to set the wordpress Avatar cache function
First, create a folder avatar in the root directory with the permission of 755. Put a new avatar (default.jpg) in it, and the default one will be used for children's shoes without portraits. The code is as follows:

Function my_avatar ($ email, $ size = '32', $ 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 '; // The size needs to be changed to the default profile picture for your website comment $ t = 2592000; // The cache validity period is 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 );}

Add the above code to the functions. php file of your topic.
Replace the get_avatar function that obtains the Avatar address with my_avatar. The following is an exception: functions. php comment list function:

get_avatar( $comment

Change:

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.

Get_avatar function introduction:
The above method is simple and convenient. However, you need to pay attention to this step. Make sure that the get_avatar function is used to call the avatar. Generally, only the old theme is not. If not, just change it.

For example:

<?php echo get_avatar( $comment->comment_author_email, $size = '48', $default = get_bloginfo('wpurl') . '/avatar/default.jpg' ); ?>

Update css/js file cache in the proxy (squid)
We generally use these four functions to add css or js files to wordpress:

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

You can define the css/js version number in the function, so that we can understand the browser cache when updating the css/js file. the default version number is the wordpress version number. The version number is linked to the complete path of css/js. after the version number is changed, the complete URL of the style loaded by css/js will also change, the browser finds that the URL change will re-request the css/js file, so that it can load the latest css/js file.

But many Agent software (such as squid) does not support "? "When we use reverse proxy to cache our website, especially after squid3.0, we have started not to include "?" . So if we want to use the squid cache function, we must remove "?", Update the cache of the squid agent can only be achieved by modifying the file name.

The following describes how to modify the js/css file name by controlling the version number in wordpress to achieve caching in the agent software:
1. add the following code to the topic code functions. php file:

/*** Description: wordpress method for updating the css/js file cache in the proxy (squid) * Author: wordpress Tutorial * Author URI: http://www.wpnoob.cn/*/function ds_filename_based_cache_busting ($ src) {// The Admin's background css/js file does not need to be processed if (is_admin () return $ src; // add the version number to the file name ". "number to distinguish return preg_replace ('/\. (js | css )\? Ver = (. +) $ /','. $2. $ 1', $ src);} add_filter ('script _ loader_src ', 'DS _ upload'); add_filter ('style _ loader_src', 'DS _ filename_based_cache_busting ');

If you are using an apache server, add the following content to 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 configuring the nginx server as follows:

location ~ ^(.+)\.(.+)\.(js|css)$ {  alias $1.$3;}

Articles you may be interested in:
  • Tips for caching Gravatar headers in WordPress locally and optimizing them

The avatar in the comment on wordpress uses Gravatar's avatar service (official registered address of Gravatar: http...

Related Article

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.