12 次查询 耗时 0.055 秒
本文共计774个字,0条留言,预计阅读时长3分钟
登录/注册
  • 首页
  • >
  • Notes
  • >
  • WordPress主题开发:头像本地缓存
  • WordPress主题开发:头像本地缓存

    作者:Misshylsay

    日期:2020年02月14日

    最近在做一个Wordpress主题,因为网站采用的Wordpress搭建,自己又不想使用官方或者别人的主题,所以就自己想做一个。

    但是,在设计制作的过程中遇到一个问题:头像有时候可以获取到,有时又获取不到。最根本的原因就是头像被墙,如果能把头像缓存到本地读取的话,这个问题就解决了。

    //头像本地缓存
    function lerm_avatar_cache( $avatar ){
        $tmp  = strpos( $avatar, 'http' );
        $g    = substr( $avatar, $tmp, strpos( $avatar, '\'', $tmp ) - $tmp );
        $tmp  = strpos( $g, 'avatar/' ) + 7;
        $f    = substr( $g, $tmp, strpos( $g, '?', $tmp ) - $tmp );//匹配图片名称
        $e    = get_template_directory() . '/avatar/' . $f . '.png';//图片缓存路径
        $t    = 604800; //缓存天数
        if( !is_file( $e ) || ( time() - filemtime( $e ) ) > $t ) copy( htmlspecialchars_decode( $g ), $e );
        else $avatar = strtr( $avatar, array( $g => get_template_directory_uri() . '/avatar/' . $f . '.png' ) );
        if( filesize( $e ) < 500 ) copy( get_template_directory()  . '/avatar/default.png', $e );
        return $avatar;
    }
    add_filter('get_avatar', 'lerm_avatar_cache' );

    把以上代码添加到主题的function.php中,然后在主题的根目录下创建一个avatar文件夹即可。

    “WordPress主题开发:头像本地缓存”共有0条评论

    发表评论

    B em del U Link Code Quote