利用Typecho解决Gravatar被墙的问题
下面来给各位介绍一个利用Typecho解决Gravatar被墙的问题,因为现在google与一些国外网站打不开所以也导致了Gravatar头像无法显示了,下面来看此问题的解决办法.
前两天Gravatar突然被墙,本来也没怎么在意,但是网站速度被拖得实在是难以忍受的地步,无奈,只好想一下解决方法,于是昨晚睡觉前百度了下,发现那些解决方案都是Wordpress的,例如@大发最新的解决方案:
function get_ssl_avatar($avatar) { $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">',$avatar); return $avatar; } add_filter('get_avatar', 'get_ssl_avatar');
但是Typecho并没有相关的文档,本想把大发的代码修改到TE,但是我不会PHP,看了好半天,翻了下程序源文件,怎么改都不行,于是我只好苦逼的修改源文件去了,希望好基友们看到后帮我鼓捣下。
好了,修改源文件其实也超级简单,只需要一行就可以了,方法如下:
打开 \var\Widget\Abstract\Comments.php 文件
在第413行找到"$url = $host . '/avatar/';"
修改为如下
//$url = $host . '/avatar/'; $url = 'https://secure.gravatar.com/avatar/';
然后保存就行了,就是把上面那个注释掉然后手动加一个链接。
最新版(1.0-14.10.10)的解决方法,同样是打开Comments.php,第397行,将下方代码:
$url = Typecho_Common::gravatarUrl($this->mail, $size, $rating, $default, $this-> request ->isSecure());
修改为:
$mailHash = NULL; if (!empty($this->mail)) { $mailHash = md5(strtolower($this->mail)); } $url = 'https://secure.gravatar.com/avatar/'; if (!empty($this->mail)) {$url .= $mailHash;} $url .= '?s=' . $size; $url .= '&r=' . $rating; $url .= '&d=' . $default; //$url = Typecho_Common::gravatarUrl($this->mail, $size, $rating, $default, $this->request->isSecure()); //"https://secure.gravatar.com"可以改成其它源。
多说的源:http://gravatar.duoshuo.com
七牛镜像源:http://avacdn.qiniudn.com
七牛镜像SSH源:https://dn-avacdn.qbox.me
当然自己搭建七牛镜像,或者反向代理Gravatar也是比较简单的,就不再赘述。
另外,如果你正在使用"评论墙"功能,那么可以在插件或者主题的function.php中找到如下代
$mostactive .= '<img class="avatar" src="http://'.$my_array[rand(0,3)].'.gravatar.com/avatar/'.md5(strtolower($value['mail'])).'?s=40&d=&r=G"/></a></li>';
将其修改为如下:
//$mostactive .= '<img class="avatar" src="http://'.$my_array[rand(0,3)].'.gravatar.com/avatar/'.md5(strtolower($value['mail'])).'?s=40&d=&r=G"/></a></li>';$mostactive .= '<img class="avatar" src="https://secure.gravatar.com/avatar/'.md5(strtolower($value['mail'])).'?s=40&d=&r=G"/></a></li>';
同样是将默认的注视,然后修改,源地址参考上面的自行修改即可。
另外,我的Wordpress的主题从GoodNice版本后就支持在设置中修改avatar的源了,方法如下:
方法二
将下方代码粘贴进入主题文件夹下的function.php内:
//自定义评论列表区域 function threadedComments($comments, $options) { $commentClass = ''; if ($comments->authorId) { if ($comments->authorId == $comments->ownerId) { $commentClass .= ' comment-by-author'; } else { $commentClass .= ' comment-by-user'; } } $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent'; ?> <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php if ($comments->levels > 0) { echo ' comment-child'; $comments->levelsAlt(' comment-level-odd', ' comment-level-even'); } else { echo ' comment-parent'; } $comments->alt(' comment-odd', ' comment-even'); echo $commentClass; ?>"> <div id="<?php $comments->theId(); ?>"> <div class="comment-author"> <?php //头像CDN by Rich $host = 'https://secure.gravatar.com'; //自定义头像CDN服务器 $url = '/avatar/'; //自定义头像目录,一般保持默认即可 $size = '32'; //自定义头像大小 $rating = Helper::options()->commentsAvatarRating; $hash = md5(strtolower($comments->mail)); $avatar = $host . $url . $hash . '?s=' . $size . '&r=' . $rating . '&d='; ?> <img class="avatar" src="<?php echo $avatar ?>" alt="<?php echo $comments->author; ?>" width="<?php echo $size ?>" height="<?php echo $size ?>" /> <cite class="fn"><?php $comments->author(); ?></cite> </div> <div class="comment-meta"> <a href="<?php $comments->permalink(); ?>"><?php $comments->date('Y-m-d H:i'); ?></a> <span class="comment-reply"><?php $comments->reply(); ?></span> </div> <?php $comments->content(); ?> </div> <?php if ($comments->children) { ?> <div class="comment-children"> <?php $comments->threadedComments($options); ?> </div> <?php } ?> </li> <? }
OK,评论区域的头像问题就可以更简单的解决了,更新程序也不用重新修改源文件
本文地址:http://www.phprm.com/code/77377.html
转载随意,但请附上文章地址:-)