


Detailed explanation of avatar cache in WordPress and cache update method in proxy, wordpress avatar_PHP tutorial
详解WordPress中的头像缓存和代理中的缓存更新方法,wordpress头像
wordpress评论中的头像是使用Gravatar的头像服务(Gravatar官方注册地址:http://en.gravatar.com),用户的缓存头像一般都是固定不变的,所以我们可以将头像缓存到本地来提高我们网站的访问速度。
我的wordpress avatar目录的头像缓存:
wordpress头像缓存功能设置方法
首先是在根目录下建立一个文件夹avatar,权限755。再在里面放一个默认的头像(default.jpg),没头像的童鞋就会用默认的。代码如下:
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'; //尺寸需要改为你自己网站评论的默认头像 $t = 2592000; // 缓存有效期30天, 这里单位:秒 if ( !is_file($e) || (time() - filemtime($e)) > $t ) { if ( !is_file($d) || (time() - filemtime($d)) > $t ) { // 验证是否有头像 $uri = 'http://www.gravatar.com/avatar/' . $f . '?d=404'; $headers = @get_headers($uri); if (!preg_match("|200|", $headers[0])) { // 没有头像,则新建一个空白文件作为标记 $handle = fopen($d, 'w'); fclose($handle); $a = $default; } else { // 有头像且不存在则更新 $r = get_option('avatar_rating'); $g = 'http://www.gravatar.com/avatar/'. $f. '?s='. $size. '&r=' . $r; copy($g, $e); } } else { $a = $default; } } $avatar = "<img alt='{$alt}' src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; return apply_filters('my_avatar', $avatar, $email, $size, $default, $alt); }
再将以上代码添加到你主题的functions.php文件。
将获取头像地址的 get_avatar 函数替换为 my_avatar 。有个例外,functions.php评论列表函数中:
get_avatar( $comment
需要改成:
my_avatar( $comment->comment_author_email
因为my_avatar函数只能通过Email来调取用户头像,所以以上情况,需要将第一个参数改成email地址。
get_avatar函数介绍:
用上面的方法简单方便啊。 不过还有一步是要注意的。得要确认在调用头像的地方都是用get_avatar函数来完成的。一般都是了,只有以前老的theme才不是。不是的话改过来就行。
如改为:
<?php echo get_avatar( $comment->comment_author_email, $size = '48', $default = get_bloginfo('wpurl') . '/avatar/default.jpg' ); ?>
代理(squid)中更新css/js文件缓存的方法
在wordpress添加css或者js文件,我们一般使用这四个函数来实现:
- wp_enqueue_script()
- wp_enqueue_style()
- wp_register_script()
- wp_register_style()
函数中你可以定义css/js的版本号,以便我们在对css/js文件更新时能够清楚浏览器的缓存,默认的版本号是wordpress的版本号。版本号会链接在css/js完整路径的后面,一般在版本号变更后,css/js载入的样式的完整URL也会变更,浏览器发现URL变更会重新请求css/js文件,这样就能达到载入最新的css/js文件。
但是很多代理软件(比如squid)并不支持”?“号形式的cache,我们在使用反向代理来cache我们的网站时,特别在squid3.0以后,已经开始不对带”?”号的url进行缓存了。所以我们如果要使用squid的缓存功能就必须去掉”?”,更新squid代理商的缓存只能通过修改文件名来实现。
以下我们将介绍在wordpress通过对版本号的控制来修改js/css文件名从而能够在代理软件中达到缓存的目的:
1、在我们的主题代码functions.php文件中添加如下代码:
/** * Description: wordpress在代理(squid)中更新css/js文件缓存的方法 * Author:wordpress教程网 * Author URI: http://www.wpnoob.cn/ */ function ds_filename_based_cache_busting( $src ) { // 管理员的后台css/js文件无需处理 if ( is_admin() ) return $src; //将版本号添加到文件名中已”.“号来区分 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' );
如果你使用的是apache服务器,在你的根目录的.htaccess文件下添加:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)\.(.+)\.(js|css)$ $1.$3 [L] </IfModule>
如果你是nginx服务器配置如下:
location ~ ^(.+)\.(.+)\.(js|css)$ { alias $1.$3; }
您可能感兴趣的文章:
- WordPress中Gravatar头像缓存到本地及相关优化的技巧

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.
