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,搞定,这篇文章已经被拉得老长了,不再啰索,收声~

  1. Pingback: Gravatar头像本地缓存|修改get_avatar()函数 | 自力博客

  2. 万哥,你这个和WILLAN大师的那个MINI版哪个更爽?我用你这个没有搞定,用那个搞定了,但是Ajax评论的时候,没刷新前头像是默认的。还有,记录访客信息前的头像,也是默认的…没明白…

  3. Pingback: 整理评论头像不显示方法

  4. Pingback: 上网记 » Blog Archive · story.tc

  5. Pingback: 三个方法解决Gravatar头像不显示的问题 | 分享互联

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

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

    • 那就用插件吧,虽说插件影响效率,但缓存到本地读取 还算可观的, 我早知道不行 :!:
      不是给你说过我实践过么 呵呵,还有以前老万 说的GZIO压缩JS CSS等 我也试验出 同一主机下 安装多个WP程序会导致某一个 的样式读取不出。 要说折腾 老万可能还比不上我 :lol: 因为我够“贱”