wordpress博客Gravatars头像缓存(插件及非插件)

上一篇我们说了有关Gravatars头像的申请和管理方面的内容,但是,在实际情况中,大家会发现,博客打开时Gravatars头像加载的速度非常慢。这是由于Gravatars头像是存在于国外主机上的图片,再加上我们特殊的国情,有时候会出现无法加载,或者加载异常缓慢的情况。怎么办呢?这一篇,我们就来讲讲解决的办法—Gravatars头像本地缓存

插件方式】下面是5个不同的Gravatars头像缓存插件:

GravatarLocalCache

FV Gravatar Cache

WP Gravatar Mini Cache

Gravatar Cache

Hacklog Gravatar Cache

使用方法:上传插件,启用插件就OK了。

注意事项:部分Gravatars头像缓存插件自带缓存图像(一般为default.jpg之类)目录权限设置777,否则可能出错。

使用插件方式来本地缓存Gravatars头像最大的好处,就是省时省力,尤其适合像本站这种对于代码头大的童鞋。

【代码方式】对于懂代码的朋友,完全可以通过添加代码的方式来解决Gravatars头像缓存的问题。

首先,在博客根目录创建一个avatar文件夹,权限为‘777’,这个就是作为本地缓存的路径,再准备一个名为default.jpg的图片(*图片大小为32*32)作为默认的头像,放在avatar文件夹下。

然后,找到wp-includes\pluggable.php文件,搜索下“function get_avatar”,将其修改为下面的代码:

1: function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { 2: $default = get_bloginfo('url').'/avatar/default.jpg'; 3: 4: if ( ! get_option('show_avatars') ) 5: return false; 6: 7: if ( false === $alt) 8: $safe_alt = ''; 9: else 10: $safe_alt = esc_attr( $alt ); 11: 12: if ( !is_numeric($size) ) 13: $size = '96'; 14: 15: $email = ''; 16: if ( is_numeric($id_or_email) ) { 17: $id = (int) $id_or_email; 18: $user = get_userdata($id); 19: if ( $user ) 20: $email = $user->user_email; 21: } elseif ( is_object($id_or_email) ) { 22: // No avatar for pingbacks or trackbacks 23: $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 24: if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) 25: return false; 26: 27: if ( !empty($id_or_email->user_id) ) { 28: $id = (int) $id_or_email->user_id; 29: $user = get_userdata($id); 30: if ( $user) 31: $email = $user->user_email; 32: } elseif ( !empty($id_or_email->comment_author_email) ) { 33: $email = $id_or_email->comment_author_email; 34: } 35: } else { 36: $email = $id_or_email; 37: } 38: 39: if ( empty($default) ) { 40: $avatar_default = get_option('avatar_default'); 41: if ( empty($avatar_default) ) 42: $default = 'mystery'; 43: else 44: $default = $avatar_default; 45: } 46: 47: if ( !empty($email) ) 48: $email_hash = md5( strtolower( $email ) ); 49: 50: if ( is_ssl() ) { 51: $host = 'https://secure.gravatar.com'; 52: } else { 53: if ( !empty($email) ) 54: $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) ); 55: else 56: $host = 'https://0.gravatar.com'; 57: } 58: 59: if ( 'mystery' == $default ) 60: $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') 61: elseif ( 'blank' == $default ) 62: $default = includes_url('images/blank.gif'); 63: elseif ( !empty($email) && 'gravatar_default' == $default ) 64: $default = ''; 65: elseif ( 'gravatar_default' == $default ) 66: $default = "$host/avatar/s={$size}"; 67: elseif ( empty($email) ) 68: $default = "$host/avatar/?d=$default&s={$size}"; 69: elseif ( strpos($default, 'http://') === 0 ) 70: $default = add_query_arg( 's', $size, $default ); 71: 72: if ( !empty($email) ) { 73: $p = '/avatar/'; 74: $f = md5(strtolower($email_hash)); 75: $a = $p . $f . '.jpg'; 76: $e = ABSPATH . $a; 77: $t = 14*24*60*60; //14天 78: if (!is_file($e) || ((is_file($e) && (time()-filemtime($e)) > $t))){ 79: $out = "$host/avatar/"; 80: $out .= $email_hash; 81: $out .= '?s='.$size; 82: 83: $rating = get_option('avatar_rating'); 84: if ( !empty( $rating ) ) 85: $out .= "&r={$rating}"; 86: 87: @copy($out, $e); 88: } 89: $out = get_bloginfo('url').$a; 90: 91: /*$out = "$host/avatar/"; 92: $out .= $email_hash; 93: $out .= '?s='.$size; 94: $out .= '&d=' . urlencode( $default );*/ 95: 96: $avatar = "{$safe_alt}"; 97: } else { 98: $avatar = "{$safe_alt}"; 99: } 100: 101: return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); 102: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

欢迎在站内搜索:网吧ip地址查询、国外空间租用、服务器托管、asp.net空间、网站备案时间、海外服务器租用的价格、ftp免费空间、论坛备案、代理 ip、移动代理ip、