趁清明小长假的时候,我折腾了非插件实现 WordPress 显示 QQ 自定义头像的功能,并且同时实现 Gravatar 头像缓存,让 Gravatar 头像与 QQ 自定义头像两者共存。很是可惜,还有那么一小小小部分没能完美实现,因为我是个完美主义者,一定要追求最完美的表现方式,所以不完美的方法我也不写了。今天还在研究此功能的时候,无意中发现,居然已经有高手折腾出来了,佩服佩服,不过对我来说还不是很完美,有兴趣的朋友可以先试试啦。
方法:
1、在对应地方(这里是wp-content/cache/img)建立权限755的文件夹,放上默认头像
2、在主题 functions.php 里面添加以下代码(<?php ?>之间的部分)
add_filter('get_avatar', 'cache', 10, 3);
function cache($avatar, $comment, $size){
$t = 604800; //設定7天, 單位:秒
$d = get_bloginfo('wpurl').'/wp-content/cache/img/default.jpg';//默认图片
$face_size = false;//单独指定头像的大小, false or integer
$qq_face_url = 'http://qun.qq.com/cgi/svr/face/getface?type=1&uin=%s';
//QQ头像地址, %s表示QQ号
if ( isset($comment->comment_author_url) && preg_match
('/^(http:\/\/)?[1-9][0-9]*$/i', $comment->comment_author_url) ) {
$qq = preg_replace('|\D*|', '', $comment->comment_author_url);
$avatar = preg_replace('/src=\'[^\']*\'/',
'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'',
$avatar);
if( $face_size )
$avatar = str_replace('\'' . $size . '\'', '\'' . $face_size . '\'', $avatar);
return $avatar;
}
else{
$f = md5(strtolower($comment->comment_author_email));
$r = get_option('avatar_rating');
$g = 'http://www.gravatar.com/avatar/'.$f.'?s=50&d='.$d.'&r='.$r;
$a = get_bloginfo('wpurl').'/wp-content/cache/img/'.$f.'.jpg';
$e = ABSPATH .'/wp-content/cache/img/'.$f.'.jpg';
if ( !is_file($e) || (time() - filemtime($e)) > $t ){
copy($g, $e);
@chmod($e,0777);
if (filesize($e) < 500) {
copy($d, $e);
}
}
if( $face_size )
$size = $face_size;
$avatar = "<img src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return $avatar;
}
return $avatar;
}
function cache($avatar, $comment, $size){
$t = 604800; //設定7天, 單位:秒
$d = get_bloginfo('wpurl').'/wp-content/cache/img/default.jpg';//默认图片
$face_size = false;//单独指定头像的大小, false or integer
$qq_face_url = 'http://qun.qq.com/cgi/svr/face/getface?type=1&uin=%s';
//QQ头像地址, %s表示QQ号
if ( isset($comment->comment_author_url) && preg_match
('/^(http:\/\/)?[1-9][0-9]*$/i', $comment->comment_author_url) ) {
$qq = preg_replace('|\D*|', '', $comment->comment_author_url);
$avatar = preg_replace('/src=\'[^\']*\'/',
'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'',
$avatar);
if( $face_size )
$avatar = str_replace('\'' . $size . '\'', '\'' . $face_size . '\'', $avatar);
return $avatar;
}
else{
$f = md5(strtolower($comment->comment_author_email));
$r = get_option('avatar_rating');
$g = 'http://www.gravatar.com/avatar/'.$f.'?s=50&d='.$d.'&r='.$r;
$a = get_bloginfo('wpurl').'/wp-content/cache/img/'.$f.'.jpg';
$e = ABSPATH .'/wp-content/cache/img/'.$f.'.jpg';
if ( !is_file($e) || (time() - filemtime($e)) > $t ){
copy($g, $e);
@chmod($e,0777);
if (filesize($e) < 500) {
copy($d, $e);
}
}
if( $face_size )
$size = $face_size;
$avatar = "<img src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return $avatar;
}
return $avatar;
}
本方法转自:http://u.wange.im/04,原作者还提供了 QQ 头像缓存的方法:http://u.wange.im/05,不过经作者测试发现获取 QQ 头像(尤其是自定义头像)与网速和服务器有很大关系,错误的话经常返回默认头像。
我暂且没有使用这个方法,只是转载过来作为收藏,等我有空再来修改成为适合我用的完美版,最近太忙了,实在没时间,以至于只能靠转载来弥补我的文囧,请读者和搜索引擎原谅我吧~
-
Wordpress gravatar 头像缓存补充说明2010年01月19日 -
Wordpress 侧边栏带头像的最新评论2009年12月26日 -
Wordpress 免插件读者墙修订版2009年12月1日 -
Wordpress 2.7+主题 gravatar 头像缓存2009年11月13日