缓存 Gravatar 头像到本地服务器
先来说说我们最终要实现的效果,在文章的评论区域和使用 WP-RecentComments 插件实现的侧栏评论区域,这些显示了 Gravatar 头像的地方,把 Gravatar 头像全部缓存到本地服务器。
如果你不清楚如何自定义 wordpress 头像,可以点击这里查看详细教程。
缓存 Gravatar 头像到本地服务器的好处
最大的好处就是加载速度,把头像缓存到本地后,打开网页后不需要再到 gravatar 网站上去查询评论者的头像,因为我们已经把头像全部缓存到本地服务器了,速度当然快。
缓存 Gravatar 头像到本地服务器的方法
更新于2011-02-16,园子发布了缓存 Gravatar 头像到本地最简单的方法,这个方法更简单些,朋友们可以试下。
本文提供的缓存 Gravatar 头像至本地服务器的方法是不用插件来实现的,所以我们得多动手去修改些代码,过程如下:
1.点击这里下载 Generic Gravatar Cache 组件。
2.解压后,在 WordPress 根目录下建立一个名为 gravatar 的文件夹,把解压后文件上传到该文件夹内(解压后的文件包含两个文件夹:cache 和 profile,三个文件:index.php, .htaccess, mysql.sql)。
3.设置 cache 文件夹为 755 权限(一般默认就是这个权限,你可以在FTP软件中右键点击该文件夹选择“属性(CHMOD)”查看权限,如不是更改为755即可)。
4.在本地新建一个 avatar.php 文件,写入下面的代码并以 utf8 格式保存,上传到 profile 文件夹内(注意要将代码中出现的网址改成你自己的)。
<?php $config['default'] = 'http://www.yzznl.cn/default-gravatar.png';//无头像时返回的默认头像路径 $config['rating'] = 'PG';//gravatar的内容级别 $config['size'] = 64;//头像尺寸 ?>
5.设计一个默认的头像,命名为 default-gravatar.png,上传到 gravatar 文件夹内,你也可以点击这里下载园子设计的图片。
6.用记事本编辑刚刚上传过的 gravatar 文件夹内 .htaccess 文件,一般只需要更改第五行,原来的代码如下:
RewriteRule . index.php [L]
你需要将它更改为你所设置的实际目录,如园子的是:
RewriteRule . /gravatar/index.php [L]
7.这一步主要是设置评论区域的 Gravatar 头像缓存到本地。打开你使用主题中的 comments.php 文件,查找类似于如下的代码:
<?php echo get_avatar( $comment, 32 ); ?>
将它替换为:
<img src="http://www.yzznl.cn/gravatar/cache/avatar/<?php echo md5(strtolower($comment->comment_author_email)); ?>" alt="" />
8.这一步目的是要把使用了 WP-RecentComments 侧栏最新评论的头像也缓存到本地。
编辑插件目录下的 Core.php 文件,查找类似如下代码:
// 当不包含默认头像时 } else { $avatar = '<div class="rc_avatar rc_' . $position . '">' . get_avatar($email, $size) . '</div>'; }
替换成:
// 当不包含默认头像时 } else { $avatar = '<div class="rc_avatar rc_' . $position . '"> <img src="http://www.yzznl.cn/gravatar/cache/avatar/'. md5(strtolower($email)) .'" alt="" width="32" height="32" /></div>'; }
请注意更改成你的网址,保存即可看到侧栏最新评论的头像的缓存效果。
Gravatar 头像只缓存一部分的解决方法
如果你发现你在进行上述操作后,Gravatar 头像只缓存一部分,那么请查看自己是否使用了 WordPress thread comment 嵌套评论插件,你需要进入 WordPress thread comment 插件的后台设置中,找到“编辑评论的HTML”选项,找到其中的调用头像部分的代码也替换成如下代码即可解决。
<img src="http://www.yzznl.cn/gravatar/cache/avatar/<?php echo md5(strtolower($comment->comment_author_email)); ?>" alt="" />
使用 mg12 的 inove 主题的特别说明
这是专门针对使用 inove 主题的朋友们写的,请忽略上述过程的第7步,将第7上变成如下操作:
打开你使用主题中的 functions.php 文件,查找类似于如下的代码:
<?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 32); } ?>
将它替换为:
<img src="http://www.yzznl.cn/gravatar/cache/avatar/<?php echo md5(strtolower($comment->comment_author_email)); ?>" alt="" width="32" height="32" />
请注意将网址替换成自己的哦。
万戈所提供的缓存 Gravatar 头像到本地服务器方法
1.前六步和上面提到的“缓存 Gravatar 头像到本地服务器的方法”一样。
2.打开 WordPress 目录下的 wp-includes/pluggable.php 文件,查找类似于如下的代码,大概是在1587行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | if ( !function_exists( 'get_avatar' ) ) : function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { if ( ! get_option('show_avatars') ) return false; if ( false === $alt) $safe_alt = ''; else $safe_alt = esc_attr( $alt ); if ( !is_numeric($size) ) $size = '96'; $email = ''; if ( is_numeric($id_or_email) ) { $id = (int) $id_or_email; $user = get_userdata($id); if ( $user ) $email = $user->user_email; } elseif ( is_object($id_or_email) ) { // No avatar for pingbacks or trackbacks $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) return false; if ( !empty($id_or_email->user_id) ) { $id = (int) $id_or_email->user_id; $user = get_userdata($id); if ( $user) $email = $user->user_email; } elseif ( !empty($id_or_email->comment_author_email) ) { $email = $id_or_email->comment_author_email; } } else { $email = $id_or_email; } if ( empty($default) ) { $avatar_default = get_option('avatar_default'); if ( empty($avatar_default) ) $default = 'mystery'; else $default = $avatar_default; } if ( !empty($email) ) $email_hash = md5( strtolower( $email ) ); if ( is_ssl() ) { $host = 'https://secure.gravatar.com'; } else { if ( !empty($email) ) $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash{0} ) % 2 ) ); else $host = 'http://0.gravatar.com'; } if ( 'mystery' == $default ) $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') elseif ( 'blank' == $default ) $default = includes_url('images/blank.gif'); elseif ( !empty($email) && 'gravatar_default' == $default ) $default = ''; elseif ( 'gravatar_default' == $default ) $default = "$host/avatar/s={$size}"; elseif ( empty($email) ) $default = "$host/avatar/?d=$default&s={$size}"; elseif ( strpos($default, 'http://') === 0 ) $default = add_query_arg( 's', $size, $default ); if ( !empty($email) ) { $out = "$host/avatar/"; $out .= $email_hash; $out .= '?s='.$size; $out .= '&d=' . urlencode( $default ); $rating = get_option('avatar_rating'); if ( !empty( $rating ) ) $out .= "&r={$rating}"; $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; } else { $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />"; } return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); } endif; |
修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | if ( !function_exists( 'get_avatar' ) ) : function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { if ( ! get_option('show_avatars') ) return false; if ( false === $alt) $safe_alt = ''; else $safe_alt = esc_attr( $alt ); if ( !is_numeric($size) ) $size = '96'; $email = ''; if ( is_numeric($id_or_email) ) { $id = (int) $id_or_email; $user = get_userdata($id); if ( $user ) $email = $user->user_email; } elseif ( is_object($id_or_email) ) { if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type ) return false; // No avatar for pingbacks or trackbacks if ( !empty($id_or_email->user_id) ) { $id = (int) $id_or_email->user_id; $user = get_userdata($id); if ( $user) $email = $user->user_email; } elseif ( !empty($id_or_email->comment_author_email) ) { $email = $id_or_email->comment_author_email; } } else { $email = $id_or_email; } $host = 'http://www.yzznl.cn'; //注意这里要修改为你的博客的地址 if ( !empty($email) ) { $out = "$host/gravatar/cache/avatar/"; $out .= md5( strtolower( $email ) ); $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; } else { $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />"; } return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); } endif; |
请注意将代码中出现的 http://www.yzznl.cn 替换成你自己的网址即可。
刷新页面即可看到头像已经缓存到本地了。
园子温馨提醒:
进行修改代码的操作时,一定要记得备份源文件,这样一旦出错可以马上恢复。
小知识:关于 Gravatar
Gravatar 的概念首先是在国外的独立 WordPress 博客中兴起的,当你到任何一个支持 Gravatar 的网站留言时,这个网站都就会根据你所提供的 Email 地址为你显示出匹配的头像。当然,这个头像,是需要你事先到Gravatar的网站注册并上传的,否则,在这个网站上,就只会显示成一个由网站管理者所设置的默认头像。

