Detailed description 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 is valid for 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:
<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 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