接下来就是运用CDN时候,你需要替换那些(静态)文件的地址,手工太麻烦了,说实在的。。。一些简单函数(比如最初的七牛镜像插件),可能并不能对所有链接进行钩子,而且可能会出错。如何用个稍微进阶的函数来解决这些问题,请看如下:

<?PHP

/*
Build by kn007
*/

define('FocusCDNHost','http://kn007.net');//The blog's web address.
define('FocusCDNRemote','http://kn007.b0.upaiyun.com');//The new URL to be used for rewriting.
define('FocusCDNIncludes','wp-content,wp-includes');//DirecTories to include in static file matching.
define('FocusCDNExcludes','.php|.xml');//Excludes something from being rewritten if one of the above strings is found in the match.
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;//String: the blog's URL
var $cdn_url = null;//String: URL of a CDN domain
var $include_dirs = null;//String: directories to include in static file matching, comma-delimited list
var $excludes = array();//Array: strings which indicate that a given element should not be rewritten (e.g. ".php")
var $rootrelative = false;//Boolean: if true, modifies root-relative links

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);
}

}

?>

该函数参考七牛镜像插件、w3tc插件等,放置于functions.php即可工作,有5个自定参数,分别为本地地址、远程地址、目录钩子、排除关键字、相对网址支持。具体应该不用多说吧?如果你有不明白的可以留言给我。

使用了该函数,你即可轻松使用cdn服务,告别手工修改

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。