Wordpress 2.7+主题 gravatar 头像缓存

  之前就给用 Wordpress 的朋友介绍过缓存 Gravatar 头像至本地服务器的方法,但是我当时介绍的方法仅适用于 Wordpress 2.7 之前的主题,不过貌似 Wordpress 2.7+ 以后,被引进了 wp_list_comments 这一函数(具体啥时候被 Wordpress 小组引进的我没有仔细研究过~),以前的头像缓存到本地的方法就不再适用了。我也一直没有注意这个问题,直到我自己把林子的 D&Z 主题升级到支持 wp_list_comments 后才发现这个问题,然后就急于寻求新的 Gravatar 头像缓存的方法。

  首先找到了兽兽的方法,看是看懂了,不过折腾了半天没折腾成功,郁闷,悟性高的童鞋可以看下:http://zou.lu/change-hosting-and-cache-gravatar/

  后来又找到了荒野无灯的方法,这家伙和Willin大师一样,超D调的,现在我明白了,真正的牛人都是不坑声的,崇拜一下。传送门:http://www.hacklog.cn/wordpress/wpskills/cache-gravatar-and-speed-up-your-blog.html

  其实 Gravatar 头像缓存的前五步和《缓存 Gravatar 头像至本地服务器》中提到的方法是一样的,我重新整理了一下,comments.php 里用 wp_list_comments 函数的同学缓存头像方法如下:

1、去作者主页下载该组件,解包,上传。路径随意,我放在 /gravatar 目录下。

2、gravatar 目录内应该有两个文件夹: cache 和 profile;三个文件: index.php, .htaccess, mysql.sql

3、赋予 cache 文件夹 755 权限: chmod a+w 。

4、本地新建一个 php 文件,写入以下内容,文件名 avatar.php,上传至 profile 文件夹内。请自行更改相应内容符合自己的需求。切记要放置默认头像。

<?php
$config['default'] = 'http://domain.com/default-gravatar.png';//无头像时返回的默认头像路径
$config['rating'] = 'PG';//gravatar的内容级别
$config['size'] = 64;//头像尺寸
?>

5、编辑 .htaccess 文件,默认 RewriteRule . index.php [L] ,请依照你的实际放置目录修改,比如我的是 RewriteRule . /gravatar/index.php [L]

6、修改 wp-includes/pluggable.php:

大概在1523行处有一个

if ( !function_exists( 'get_avatar' ) ) :
/**
* Retrieve the avatar for a user who provided a user ID or email address.
*
* @since 2.5
* @param int|string|object $id_or_email A user ID,  email address, or comment object
* @param int $size Size of the avatar image
* @param string $default URL to a default image to use if no avatar is available
* @param string $alt Alternate text to use in image tag. Defaults to blank
* @return string <img> tag for the user's 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.yourdomain.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;

  OK,搞定,这篇文章已经被拉得老长了,不再啰索,收声~

本文已盖 71 层楼

  1. 回复 老七 说:

    提高速度不错~

  2. 回复 蛋卷 说:

    头像缓存我还不太会用

  3. 现在是如何清除缓存。

  4. 回复 少爷 说:

    费解啊,为什么一定要缓存到本地呢?又费流量又费空间。
    很多人热衷于介绍这个技术。

  5. 回复 Leeiio 说:

    要hack pluggable.php文件的始终不是好方法啊,一直用着shawn大师介绍的那种,真真的缓存各种尺寸的头像图片哈

  6. 回复 huaimao 说:

    :mrgreen: 偶是专程前来感谢地!

  7. 回复 阿修 说:

    办法不错,一直本地缓存的~~

  8. 回复 Leo.N 说:

    Willin大师那个一直可以用,也比较简单。。

  9. zblog 应该怎么做呢 ?

  10. 回复 先看看 说:

    这个用代码实现,还不错

  11. 回复 园子 说:

    我也很懒
    不想折腾了~

  12. 回复 510博客 说:

    性开放的后果,发到博客里去了!

  13. 回复 Code-k 说:

    学习了~呵呵~

  14. 呵呵,我刚从他的博客过来的。。。

  15. 回复 SATURN 说:

    华丽的飘过。。。

  16. 回复 chancat 说:

    现在又好了.. 你上QQ一下

  17. 回复 huaimao 说:

    这个我就不折腾了!我是懒人一个!

  18. 回复 chancat 说:

    老万,发现你这,评论者 的链接转向 不能打开 只转到你的url上,

  19. 回复 Awu 说:

    还没关注头像缓存!

  20. 回复 66 说:

    暂时还未研究到这块~
    万哥能做个链接么,上星期链上你了

  21. 回复 zwwooooo 说:

    willin大师的不就可以了么?我从一开始折腾自己主题用的就是wp_list_comments,我现在用的就是willin的方法

  22. 回复 huangjun 说:

    Gravatar 头像保存到本地确实很好,那样页面打开速度又可以快一些了~

  23. 回复 超人 说:

    万哥又想来害我了..上次就这个家伙还我还原了.诶. 忍不住我又要来试试 :arrow:

  24. 回复 小y 说:

    我决定用兽兽推荐的插件~~~不折腾了~

  25. 回复 Showfom 说:

    现在不用这么麻烦了 一个插件搞定:
    http://philna.com/2009/10/cache-avatar/
    而且是全局的,后台的头像也缓存了。

  26. 回复 qiqiboy 说:

    这和whisperer介绍的有什么不同吗

  27. 回复 林木木 说:

    又动了WordPress的程序文件……偶主题采用的是评论functions自定义回调方式,直接那里改,HOHO

  28. 回复 林木木 说:

    chrome+GR插件=沙发专业户!

  1. 我的Wordpress,这些一个都不能少(一) | 阿邙’S Blog

Leave a comment

您已输入0

三言两语

我的生活心情语录

工作手札

路漫漫其修远兮

精品推荐

好东西要分享

嘻哈娱乐

八卦趣事一笑而过

电脑网络

全新技术尽搜罗

转来载去

文人墨客美文赏析