Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the update method of avatar cache and proxy cache in WordPress

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how to achieve WordPress avatar cache and proxy cache update method, I believe most people still do not know how to share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!

The avatars in wordpress comments are avatars using Gravatar's avatar service (Gravatar official registration address: http://en.gravatar.com). Users 'cached avatars are generally fixed, so we can cache avatars locally to improve our website's access speed.

Avatar cache for my wordpress avatar directory:

Wordpress avatar cache function setting method

The first is to create a folder avatar in the root directory, permissions 755. Then put a default avatar inside (default.jpg), children's shoes without avatars will use the default. 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'; //size needs to be changed to default avatar for your own website reviews $t = 2592000; //cache validity period 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])) { //If there is no avatar, create a blank file as a mark $handle = fopen($d, 'w'); fclose($handle); $a = $default; } else { //update if there is an avatar and it does not exist $r = get_option('avatar_rating'); $g = 'http://www.gravatar.com/avatar/'. $f. '? s='. $size. '&r=' . $r; copy($g, $e); } } else { $a = $default; } } $avatar = "

{$alt}

"; return apply_filters('my_avatar', $avatar, $email, $size, $default, $alt);}

Add the above code to your theme's functions.php file.

Replace the get_avatar function that gets the avatar address with my_avatar. There is one exception, functions.php comment list function:

get_avatar( $comment

It should read:

my_avatar( $comment->comment_author_email

Because the my_avatar function can only call the user avatar through Email, so in the above case, you need to change the first parameter to email address.

Get_avatar function description:

The above method is simple and convenient. But there is one more step to watch out for. Make sure you use the get_avatar function wherever you call avatars. Generally, it is, only the old theme is not. If not, just change it.

For example read

The requested URL/css/js was not found on this server.

To add css or js files in wordpress, 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 clear the browser cache when updating css/js files. The default version number is wordpress version number. The version number will be linked to the back of the css/js full path. Generally, after the version number is changed, the full URL of the css/js loaded style will also change. The browser will re-request the css/js file if it finds the URL change, so that it can load the latest css/js file.

But many proxy software (such as squid) do not support "? cache in the form of "#", we use reverse proxy to cache our website, especially after squid3.0, has begun to do not "#" The URL of the "number" is cached. So if we want to use squid cache function, we must remove "? ", updating the squid agent cache can only be done by modifying the file name.

Here's how to modify js/css file names in wordpress by controlling version numbers to cache them in proxy software:

1. Add the following code to our theme code functions.php file:

/** * Description: wordpress in proxy (squid) update css/js file cache method * Author:wordpress tutorial network * Author URI: http://www.wpnoob.cn/ */function ds_filename_based_cache_busting( $src ) { //administrator background css/js file does not need to process if ( is_admin() ) return $src; //add version number to file name already ". 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 the following 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 an nginx server configured as follows:

location ~ ^(.+)\. (.+)\. (js|css)$ { alias $1.$ 3;} The above is "How to achieve WordPress avatar cache and proxy cache update method" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report