互联网技术 · 2024年2月14日

Typecho教程:更换全球头像地址并输出评论调用代码

Typecho的评论默认使用的是Gravata头像,每次页面打开总是卡在gravatar.com的链接,严重影响加载速度,所以必须使用国内比较快的地址进行干预,以达到更好的用户体验。林三之前是修改的源码,现在又学习了主题层面的修改方法,这里和大家分享下,也是为自己做个笔记,方便日后使用和调整。以下内容部分来自百度,部分来自initial主题!

一、通过修改程序源码来替换Gravatar头像地址

打开var/Typecho/Common.php,通过搜索”gravatar”找到下面的代码,大概在939行。

public static function gravatarUrl($mail, $size, $rating, $default, $isSecure = false) { if (defined(__TYPECHO_GRAVATAR_PREFIX__)) { $url = __TYPECHO_GRAVATAR_PREFIX__; } else { $url = $isSecure ? https://secure.gravatar.com : http://www.gravatar.com; $url .= /avatar/; } if (!emptyempty($mail)) { $url .= md5(strtolower(trim($mail))); } $url .= ?s= . $size; $url .= &r= . $rating; $url .= &d= . $default; return $url; } 修改上面的第6、7行即可,也就是将默认的两个地址进行替换,比如换成这样:

$url = $isSecure ? https://cdn.v2ex.com/gravatar/ : http://gravatar.duoshuo.com/avatar/; 请注意!两行变成了1行,是因为这两个地址的头像目录不同,所以直接合并写成了1个网址。

二、通过主题 functions.php 新增函数来替换Gravatar头像地址

打开 functions.php 第2行添加 define(__TYPECHO_GRAVATAR_PREFIX__, https://cdn.v2ex.com/gravatar/); 即可实现地址更改,不需要在 config.inc.php 中修改源文件。如果想要更换成其它地址,修改 v2ex 地址为其它即可。

林三为了让主题更加质感,将地址修改功能集成到主题设置里面,需要在 functions.php 的 function themeConfig($form) 内增选项。基本逻辑是,如果没有选择使用哪个国内地址,则默认使用官方的头像地址。

文章来源:林三在线 来源地址:https://linsan.net/change-gravatar-site-for-typecho.html

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册