非插件实现wordpress的七牛CDN全站加速,水煮鱼有一个七牛的插件,也有人有super cache来用七牛CDN。现在给出一个不要插件的方法,插件能少一个就少一个吧!
把以下代码加入functions.php中:位置一定要放对,在<?php 后面一行就好。
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465//Code from http://seavia.com///七牛CDNdefine('FocusCDNHost','http://www.seavia.com');//wordpress网站网址define('FocusCDNRemote','http://seavia.qiniudn.com');//cdn域名define('FocusCDNIncludes','wp-content,wp-includes');//设置加速目录define('FocusCDNExcludes','.php|.xml');//设置文件白名单define('FocusCDNRelative','');//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog's domain as prefix. 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);} }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。