全面启用gravatar头像缓存

2009/09/14 Add 57 comments |

    把Gravatar头像缓存到本地服务器,避免访客每次都从gravatar.com下载头像,有效提升了博客访问速度。因为我用的是国内空间,所以启用头像缓存有一个更重要的意义:让用教育网的同学也可以看到头像。Let's whisper有两个地方用到了gravatar头像:评论列表和最新评论(启用了wp-recentcomments插件),所以这两处都要进行缓存。

    我用的启用gravatar头像缓存的方法来自willin,取名为“小g”,也许这是世界上最小巧的gravatar头像缓存方案。下面我就介绍一下这个方法,特别是iNove用户,往下看咯。

1、对评论列表里的gravatar头像启用缓存
先在你的网站 wp-content 的同级目录 建立文件夹: /avatar 权限:755, 这是准备 gravatar 缓存的路径。
找到原来的 comments.php 或是在 functions.php 里的 function mytheme_comment($comment, $args, $depth) 会有一段像这样:

<?php echo get_avatar($comment,$size='50',$default='<path_to_url>' ); ?>

如果你用的iNove主题,在functions.php里会有下面的一段:

<?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 32); } ?>

把这个换成:

<?php
$p = 'avatar/';
$f = md5(strtolower($comment->comment_author_email));
$a = $p . $f .'.jpg';
$e = ABSPATH . $a;
if (!is_file($e)){    //当头像不存在就更新
$d = get_bloginfo('url'). '/avatar/default.jpg';
$s = '50';
$r = get_option('avatar_rating');
$g = 'http://www.gravatar.com/avatar/'.$f.'.jpg?s='.$s.'&d='.$d.'&r='.$r;
copy($g, $e);
if ( filesize($e) == 0 ){ copy($d, $e); }
};
?>
<img src='<?php echo $a ?>' alt='' />

预设头像 default.jpg, 放在刚刚建立的 avatar 文件夹内,当访客不是gravatar用户时就会调用这个头像。 上面的变量名顺便解释一下: $p 是 path(路径), $f 是 filename(档案名), $a 是 avatar(缓存档案), $e 是服务器路径, $d 是 default(预设头像), $s 是 size(尺寸), $r 是 rate(分级), $g 是 gravatar(原始档案)。

如果想要更新头像,去ftp里把 /avatar 路径下的头像文件删除就可以了。

2、对wp-recentcomments插件的头像启用缓存

/wp-recentcomments 路径下找到 core.php,其中有这么一段:

function rc_get_avatar($show, $position, $size, $default, $email) {

    $avatar = '';
    if ($show == 'true' && function_exists('get_avatar') && get_option('show_avatars')) {

        // 当默认头像来自 Internet 时
        if (substr(strtolower($default), 0, 7) == 'http://') {
            $file = attribute_escape($default);
            $avatar = '<div class="rc_avatar rc_' . $position . '">' . get_avatar($email, $size, $file) . '</div>';

        // 当默认头像来自 "/wp-recentcomments/avatars/" 目录时
        } else if ($default != '') {
            $file = 'wp-content/plugins/wp-recentcomments/avatars/' . $default;
            if (file_exists($file)) {
                $file = get_bloginfo('siteurl') . '/' . $file;
                $avatar = '<div class="rc_avatar rc_' . $position . '">' . get_avatar($email, $size, $file) . '</div>';
            }

        // 当不包含默认头像时
        } else {
            $avatar = '<div class="rc_avatar rc_' . $position . '">' . get_avatar($email, $size) . '</div>';
        }
    }

    return $avatar;
}

改成:

function rc_get_avatar($show, $position, $size, $default, $email) {

    $avatar = '';
    if ($show == 'true' && function_exists('get_avatar') && get_option('show_avatars')) {

$p = 'avatar/';
$f = md5(strtolower($email));
$a = $p . $f .'.jpg';
$e = ABSPATH . $a;
if (!is_file($e)){
if ($default){$d = $default;} else {$d = get_bloginfo('url'). '/avatar/default.jpg';}
$r = get_option('avatar_rating');
$g = 'http://www.gravatar.com/avatar/'.$f.'.jpg?s='.$size.'&d='.$d.'&r='.$r;
copy($g, $e);
if ( filesize($e) == 0 ){ copy($d, $e); }
};
    $avatar = '<div class="rc_avatar rc_' . $position . '"><img src="'.$a.'" style="width:'.$size.'px;height:'.$size.'px;" alt="" /></div>';

    }
    return $avatar;
}

完成。

重要提示:  如果采用这个方法后,文章页面无法显示头像,而且头像地址变成形如www.whisperer.name/cache-gravatar/avatar/...的样子,请把上面代码中的$p = 'avatar/'改成$p = '/avatar/'

Posted by whisperer | in Skills, wordpress | 2,478 views | Tags: , , | Twit it !
  1. 02. 1st, 2010 at 14:10 | #1

    为什么很多都能实现本地缓存,但是个别的就不行呢

  2. 04. 18th, 2010 at 19:19 | #2

    我想问下,上面的代码要是放到function.php里面在调用的话,改怎么写呢?

  3. 07. 31st, 2010 at 17:21 | #3

    为什么我的wordpress无法拉取头像呢。

  4. 08. 4th, 2010 at 23:59 | #4

    我的是国外空间,不知道直接下载快,还是缓存下载快

Comment pages
  1. 09. 25th, 2009 at 08:05 | #1
  2. 02. 25th, 2010 at 00:46 | #2
  3. 04. 13th, 2010 at 13:55 | #3