WordPress 头像本地化缓存最有效方法

2017-06-05 07:12 1,862 22 条评论 龙笑天下
Dragon主题购买

WordPress 头像本地化缓存最有效方法

由于 gravatar头像被墙,导致头像加载很慢,因此在网上搜索“wordpress 头像缓存优化”的方法,结果搜到的基本都是不有效,甚至是不可用的,偶然在露兜博客看到了一个方法,试用后,感觉非常完美,在此就分享出来供大家借鉴一二。

优化后的缓存代码

废话不多说,直接上代码:

/**
 * WordPress 头像本地化缓存最有效方法 - 龙笑天下
 * https://www.ilxtx.com/wordpress-gravatar-local-cache.html
 */
function dr_get_avatar( $email = 'gravatarcache@ilxtx.com', $size = '32', $default = '', $alt = '') {
    // Gravatar 头像源,可自行修改
    $gravatar_sever = 'dn-qiniu-avatar.qbox.me';
    $gravatar_uri = 'https://'.$gravatar_sever;

    // 设置$email 默认值为一个不存在的邮箱,如:gravatarcache@ilxtx.com,防止空的$email 导致出错
    $f = md5( strtolower( $email ) );

    // 以下代码将头像缓存到 wp-content 目录下
    if ( !is_dir( WP_CONTENT_DIR.'/avatar' ) ) {
        mkdir(WP_CONTENT_DIR.'/avatar', 0755, true);
    }
    $a = WP_CONTENT_URL.'/avatar/'. $f.$size.'.png';
    $e = WP_CONTENT_DIR.'/avatar/'. $f.$size.'.png';
    $d = WP_CONTENT_DIR.'/avatar/'. $f.'-d.png';

    if($default==''){
        $default = 'https://cdn.ilxtx.com/wp-content/themes/dragon/assets/images/gravatar.png';
    }

    $t = 2592000; // 缓存有效期 30 天, 这里单位:秒
    if ( !is_file($e) || (time() - filemtime($e)) > $t ) {
        if ( !is_file($d) || (time() - filemtime($d)) > $t ) {
            // 验证是否有头像
            $uri = $gravatar_uri.'/avatar/'.$f.'?d=404';
            $headers = @get_headers($uri);
            if (!preg_match("|200|", $headers[0])) {
                // 没有头像,则新建一个空白文件作为标记
                $handle = fopen($d, 'w');
                fclose($handle);

                $a = $default;
            } else {
                // 有头像且不存在则更新
                $r = get_option('avatar_rating');
                $g = $gravatar_uri.'/avatar/'. $f.'?s='.$size.'&r='.$r;
                copy($g, $e);
            }
        } else {
            $a = $default;
        }
    }

    $avatar = "<img alt='{$alt}' src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";

    return apply_filters('dr_get_avatar', $avatar, $email, $size, $default, $alt);
}

使用方法

  1. 重要:在 wp-content 目录下新建一个目录,命名为 avatar,权限设为 755;如果选择头像缓存到当前主题下,请在当前主题目录下,请在当前主题目录下新建一个目录,命名为 avatar,权限设为 755;
  2. 将以上代码放到当前主题 functions.php 的最后一个 ?> 的前面;
  3. 以上代码第 22 行换成你本地的默认头像地址;
  4. 这里默认头像大小是 32*32,可在以上代码第 1 行更改;
  5. 在需要使用 Gavatar 头像的地方调用 dr_get_avatar 函数即可。一般在主题所有文件中,将 get_avatar 函数替换为 dr_get_avatar。有个例外,functions.php 评论列表函数中:get_avatar( $comment 需要改为 dr_get_avatar( $comment->comment_author_email
  6. 上面代码里用到的 Gravatar 头像镜像源是dn-qiniu-avatar.qbox.me,如果你想替换为其他源的话,可以看此文:解决国内 Gravatar 头像被墙无法显示问题,另附 Gravatar 头像国内镜像源地址大全

因为 dr_get_avatar 函数只能通过 Email 来调取用户头像,所以以上情况,需要将第一个参数改成 email 地址。

友情提示

这个新的头像调用函数 dr_get_avatar 可以传递 alt 参数,因此可以不必使用下文中这个给 gravatar 头像加 alt 标签的代码了:

给 wordpress 头像加上 alt 标签-BG
给 wordpress 头像加上 alt 标签

给 wordpress 头像加上 alt 标签

不知道为什么,wordpress 几乎所有的博客头像标签都没加 alt!但是懂 SEO 的都知道,图片的 alt 属性是一定要加的,搜索引擎的爬虫是无法读取图片上到底是什么内容的,只能靠它的 alt 标...

另外,本站Dragon 主题已经自带集成了此功能,更加强大,欢迎购买体验:

Dragon 主题:Wordpress 商城会员多功能高级主题 V5.0.0【拼团购更优惠哦~】-BG
Dragon 主题:Wordpress 商城会员多功能高级主题 V5.0.0【拼团购更优惠哦~】

Dragon 主题:Wordpress 商城会员多功能高级主题 V5.0.0【拼团购更优惠哦~】[出售]

WordPress 个人博客 CMS 主题 Dragon,CMS、博客、自适应响应式、用户中心、商城,强大到窒息~

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2017-05-27

2017-06-28

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more