文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。
Gravatar(Globally Recognized Avatar),全球公认头像,是一项免费的服务,用邮箱注册以后,可以上传自己的头像,之后在任何支持Gravatar(比如WordPress)平台上使用邮箱评论时,都会自动调用你在Gravatar上设置的头像。如果需要修改头像,只需要再次登录Gravatar修改就好了,之前所有的评论头像都会跟着修改
如果你没有设置Gravatar,系统会根据系统设置自动放置一个默认头像,这个默认头像可以通过回调函数来实现,比如:
http://www.gravatar.com/avatar/91ec0272dd603a7749f0f6f56bf33862?s=36&d=http://www.slyar.com/blog/wp-content/themes/d8/img/default.png&r=g
这里会先尝试通过UID=91ec0272dd603a7749f0f6f56bf33862调取你的Gravatar头像,如果头像不存在,则使用默认头像这里比如http://www.slyar.com/blog/wp-content/themes/d8/img/default.png
详细参数查看 Codex: Using Gravatars
然而某些模板/主题的设计师并没有考虑性能问题,尽管很多人都没有设置Gravatar头像,尽管最后结果都是默认头像,但是由于回调函数的缘故,浏览器并不认为这些URL都是指向同一个默认头像的,在这些头像的request header中你能看到Vary: Accept-Encoding,所以对于所有的默认头像,浏览器都会当成是unique的请求,无法缓存,而且也增加了无谓的请求数目
下面开始hack,我只拿我现在用的模板举例,文件都在模板根目录下:
./function.php
增加一个函数validate_gravatar,根据邮箱判断头像是否存在,不存在返回404,存在返回200
修改原本的头像显示代码,头像存在调用头像,头像不存在直接返回默认头像URL,不使用回调函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
function validate_gravatar($email) { $hash = md5(strtolower(trim($email))); $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404'; $headers = @get_headers($uri); if (!preg_match("|200|", $headers[0])) { $has_valid_avatar = FALSE; } else { $has_valid_avatar = TRUE; } return $has_valid_avatar; } //评论样式 function deel_comment_list($comment, $args, $depth) { echo '<li '; comment_class(); echo ' id="comment-'.get_comment_ID().'">'; //头像 echo '<div class="c-avatar">'; if (validate_gravatar($comment->comment_author_email)) { echo str_replace(' src=', ' data-original=', get_avatar( $comment->comment_author_email, $size = '36' , deel_avatar_default())); } else { echo "<img alt='' src='" . deel_avatar_default() . "' class='avatar avatar-36' height='36' width='36' />"; } echo '</div>'; // ....... } |
./widgets/wid-readers.php
侧边栏同理,因为validate_gravatar已经写在function.php里面了,这里不用重写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function slyar_get_avatar($email) { if (validate_gravatar($email)) { return str_replace(' src=', ' data-original=', get_avatar( $email, $size = '36' , deel_avatar_default())); } else { return "<img alt='' src='" . deel_avatar_default() . "' class='avatar avatar-36' height='36' width='36' />"; } } function mod_newcomments( $limit,$outpost,$outer ){ global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,40) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_post_ID!='".$outpost."' AND comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $limit"; $comments = $wpdb->get_results($sql); foreach ( $comments as $comment ) { $output .= '<li><a href="'.get_permalink($comment->ID).'#comment-'.$comment->comment_ID.'" title="'.$comment->post_title.'上的评论">'.slyar_get_avatar($comment->comment_author_email).strip_tags($comment->comment_author).' <span class="muted">'.timeago( $comment->comment_date_gmt ).'说:<br>'.str_replace(' src=', ' data-original=', convert_smilies(strip_tags($comment->com_excerpt))).'</span></a></li>'; } echo $output; }; |
写完测试一下,所有的默认头像现在都是统一本地URL了,缓存工作良好
转载请注明:Slyar Home » 解决Gravatar默认头像无法缓存问题(Vary: Accept-Encoding)