^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

xiu主题启用七牛cdn缓存后无法评论解决办法

之前陌小雨分享了一个启用七牛后知更鸟主题无法评论解决办法,这不又换回了我喜欢的 xiu 主题么,发现还是有这个问题,因为模版结构的问题,按那个办法无法解决,经过摸索,只好通过代码方式然后辅以陌小雨我聪明的大脑,就实现了骑牛的 cdn 缓存啦:有需要的朋友可以借鉴下:

//水煮鱼的七牛 CDN 插件代码版
define('FocusCDNHost','https://dedewp.com'); //wordpress 网站网址
define('FocusCDNRemote','http://cdndedewp.qiniudn.com'); //cdn 域名
define('FocusCDNIncludes','wp-content/uploads'); //设置加速目录
define('FocusCDNExcludes','.php|.xml|.html|.po|.mo|.js');  //设置文件白名单
define('FocusCDNRelative','');
function do_cdnrewrite_ob_start() {
    $rewriter = new FocusCDNRewriteWordpress();
    $rewriter->register_as_output_buffer();
}
add_action('template_redirect', 'do_cdnrewrite_ob_start');
class FocusCDNRewriteWordpress extends FocusCDNRewrite
{
    function __construct() {
        $excl_tmp = FocusCDNExcludes;
        $excludes = array_map('trim', explode('|', $excl_tmp));
        parent::__construct(
            FocusCDNHost,
            FocusCDNRemote,
            FocusCDNIncludes,
            $excludes,
            !!FocusCDNRelative
        );
    }
public function register_as_output_buffer() {
    if ($this->blog_url != FocusCDNRemote) {
        ob_start(array(&$this, 'rewrite'));
        }
    }
}
class FocusCDNRewrite {
    var $blog_url    = null;
    var $cdn_url     = null;
    var $include_dirs   = null;
    var $excludes    = array();
    var $rootrelative   = false;
function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
    $this->blog_url   = $blog_url;
    $this->cdn_url    = $cdn_url;
    $this->include_dirs  = $include_dirs;
    $this->excludes   = $excludes;
    $this->rootrelative  = $root_relative;
}
protected function exclude_single(&$match) {
    foreach ($this->excludes as $badword) {
        if (stristr($match, $badword) != false) {
            return true;
        }
    }
    return false;
}
protected function rewrite_single(&$match) {
    if ($this->exclude_single($match[0])) {
        return $match[0];
    } else {
    if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
        return str_replace($this->blog_url, $this->cdn_url, $match[0]);
    } else {
        return $this->cdn_url . $match[0];
        }
    }
}
protected function include_dirs_to_pattern() {
    $input = explode(',', $this->include_dirs);
    if ($this->include_dirs == '' || count($input) < 1) {
        return 'wp\-content|wp\-includes';
    } else {
        return implode('|', array_map('quotemeta', array_map('trim', $input)));
    }
}
public function rewrite(&$content) {
$dirs = $this->include_dirs_to_pattern();
$regex = '#(?<=[(\"\'])';
$regex .= $this->rootrelative
? ('(?:'.quotemeta($this->blog_url).')?')
: quotemeta($this->blog_url);
$regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
}
} 

应该可以把插件的目录也一并加速的,剩下的你们就自己鼓捣去吧!哈哈。

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

赠人玫瑰,手有余香。