Gravatar头像缓存再次修正版
2010 四月 29
以前写过一篇文章叫《缓存Gravatar 头像,博客再次提速》,当初是直接通过修改wp函数来实现的,如果升级了WP版本,每次都要修改,因此还是有一点不方便。
这次补充的主要是通过插件机制来实现显示缓存头像,而不是修改WP文件.另外,新增 qq头像显示功能。
说明:用此修正版的代码前先要确保已经启用了GRAVATAR CACHE 程序。具体方法可见 《缓存Gravatar 头像,博客再次提速》
修正版本:
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 | ######## START ############头像缓存#########BY 荒野无灯############### //2010-05-08更新 function cache_avatar($avatar, $id_or_email, $size) { $host=get_bloginfo('wpurl'); $email = ''; $url=''; 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; $url=$id_or_email->comment_author_url; } } else { $email = $id_or_email; } //如果评论者的博客是Qzone ,就用TA的Q头像。 if ( !empty($url) && preg_match('/^(http:\/\/)?[1-9][0-9]*[\.a-zA-Z\/]+$/i', $url) || preg_match('/^[1-9][0-9]*@qq\.com$/i', $email)) { $qq_face_url = 'http://qun.qq.com/cgi/svr/face/getface?type=1&uin=%s'; //QQ头像地址, %s表示QQ号 $qq = preg_replace('|\D*|', '', $url); if(!$qq) $qq = preg_replace('|\D*|', '', $email); $avatar = preg_replace('/src=\'[^\']*\'/', 'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'', $avatar); } else { //否则就用gravatar头像 $out = "$host/wp-content/gravatar_cache/cache/avatar/"; $out .= md5( strtolower( $email ) ); $avatar = "<img src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; } return $avatar; } add_filter('get_avatar', 'cache_avatar', 50,3); ######## END ############头像缓存###########BY 荒野无灯############# |
最初版本:
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 | ######## START ############头像缓存#########BY 荒野无灯############### function cache_avatar($avatar, $comment, $size) { $host=get_bloginfo('wpurl'); $qq_face_url = 'http://qun.qq.com/cgi/svr/face/getface?type=1&uin=%s'; //QQ头像地址, %s表示QQ号 //如果评论者的博客是Qzone ,就用TA的Q头像。 if ( isset($comment->comment_author_url) && preg_match('/^(http:\/\/)?[1-9][0-9]*[\.a-zA-Z\/]+$/i', $comment->comment_author_url) || preg_match('/^[1-9][0-9]*@qq\.com$/i', $comment->comment_author_email)) { $qq = preg_replace('|\D*|', '', $comment->comment_author_url); if(!$qq) $qq = preg_replace('|\D*|', '', $comment->comment_author_email); $avatar = preg_replace('/src=\'[^\']*\'/', 'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'', $avatar); } else { //否则就用gravatar头像 $out = "$host/wp-content/gravatar_cache/cache/avatar/"; $out .= md5( strtolower( $comment->comment_author_email ) ); $avatar = "<img src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; } return $avatar; } add_filter('get_avatar', 'cache_avatar', 50,3); ######## END ############头像缓存###########BY 荒野无灯############# |
29 Responses
Post a comment






你好,我想问一下,怎样才能修改成只识别Qmail而不识别url中的qq号码?
注释掉QQ相关即可,我现在用的:
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
//2010-05-08更新
function cache_avatar($avatar, $id_or_email, $size)
{
$host=get_bloginfo('wpurl');
$email = '';
$url='';
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;
$url=$id_or_email->comment_author_url;
}
} else {
$email = $id_or_email;
}
//如果评论者的博客是Qzone ,就用TA的Q头像。
//preg_match('/^(http:\/\/)?[1-9][0-9]*[\.a-zA-Z\/]+$/i', $url)
//http://user.qzone.qq.com/595529846
//http://595529846.qzone.qq.com/
//现已不能获取头像,注释掉之
/*
if (
!empty($url) && (preg_match('/^(http:\/\/)?user\.qzone\.qq\.com\/([1-9][0-9]+)$/i', $url ,$matches) || preg_match('/^(http:\/\/)?([1-9][0-9]+)\.qzone\.qq\.com\/?$/i', $url ,$matches))
||
preg_match('/^[1-9][0-9]*@qq\.com$/i', $email)
)
{
//$qq_face_url = 'http://face8.qun.qq.com/cgi/svr/face/getface?type=1&uin=%s';
$qq_face_url = 'http://face1.qun.qq.com/cgi/svr/face/getface?cache=0&type=1&fid=0&uin=%s&vfwebqq=2a3080d85214e831f30458971fdfa864d52f635d8e0ecb50a4bfc7fc8411a3d089519c0e3752e105';
//QQ头像地址, %s表示QQ号
//$qq = preg_replace('|\D*|', '', $url);
if( is_numeric($matches[2]) )
$qq=$matches[2];
else
$qq = preg_replace('|\D*|', '', $email);
$avatar = preg_replace('/src=\'[^\']*\'/', 'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'', $avatar);
}
else
{
*/
//否则就用gravatar头像
$out = "$host/wp-content/gravatar_cache/cache/avatar/";
$out .= md5( strtolower( $email ) );
$avatar = "<img src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
/* } */
return $avatar;
}
add_filter('get_avatar', 'cache_avatar', 50,3);
######## END ############头像缓存###########BY 荒野无灯#############
@荒野无灯
我知道了,谢谢
祝你中秋happy
恩,现在还能调用qq头像吗?
可以不加载那个开头那个啥程序就缓存了不啊?
这个本来就是调用那个程序的。如果不想要那个,可以试下这个喽:gravatar头像缓存插件——Hacklog Gravatar Cache
博主,这个缓存的头像可以修改大小吗?这个缓存出来的头像在我的博客很模糊,可以修改下的吗?除了这个遗憾其他都很好用的,网站加载快了很多
这个大小可以修改的。
试试效果,谢谢!!!
蛮不错,这个插件比较小巧,而且适用……我用willin的方法css得改,反正是很麻烦
咱来测试QQ头像显示.
刚刚我那里不行.
但是既不现实原头像 也不现实自定义头像
填qq邮箱要是能显示自定义头像就好啦
友情测试下哈
不是自定义的qq头像啊
测试
跟其他的QQ头像显示的方式不太一样哦,其他只要在网址处填入QQ号就可以了哦。
我觉得这个更实用,如果你填写的邮箱是QQ邮箱(带号码的),就显示QQ头像,否则,再看你填写的博客地址是不是QQ空间,如果是则同样显示QQ头像。
试下QQ头像,尝试修改玩玩。
荒野大哥,这段代码是插到主题的functions.php里吗?
嗯,和以前一样,老规矩。
试一下QQ头像,哈哈.
添qq邮箱,测试
童鞋,QQ邮箱只能用QQ号的,如果用非QQ号码的QQ邮箱是没办法识别的。
听课要靠前坐。
填其它邮箱地址,网址填QQ空间(测试)
填 QQ邮箱,网址不填QQ空间。(测试)