当前位置:图集之家知识

WordPress非插件纯代码实现静态资源CDN加速功能(又拍云、七牛)

WordPress非插件纯代码实现静态资源CDN加速功能(又拍云、七牛)

内容

七牛云存储(又拍云存储)是一个静态文件CDN加速服务,七牛存储分布全国的 CDN 加速网络,采用电信、联通、移动、教育网四线带宽,不限制带宽峰值,并通过高速缓存、智能解析、负载均衡技术,为用户计算最快访问机房,与传统的IDC相比,图片打开速度提升60%-300%。

既然我们用上了七牛的源站加速这一功能,不如将wordpress七牛镜像存储插件,WP SUPER CACHE等里面的CDN功能,都用代码方式实现。

wordpress免插件纯代码实现又拍云、七牛CDN加速:

又拍云设置CDN:

在七牛或者又拍云设置好CDN后,将下面代码仍入主题的functions.php函数文件中即可。

将本地图片地址替换为CDN地址:

添加至主题目录functions.php中:

define(CDN_HOST,CDN网址);

add_filter(the_content,z_cdn_content);

function z_cdn_content($content){

return str_replace(home_url()./wp-content/uploads, CDN_HOST./wp-content/uploads, $content);

}

add_filter(wp_get_attachment_url,z_get_attachment_url,10,2);

function z_get_attachment_url($url, $post_id){

return str_replace(home_url(), CDN_HOST, $url);

}

注意 define(CDN_HOST,CDN网址); 需要替换为你自己的CDN地址。

将主题静态资源地址替换为CDN地址

添加至主题目录functions.php中:

add_filter(stylesheet_directory_uri,z_cdn_stylesheet_directory_uri,10,3);

function z_cdn_stylesheet_directory_uri($stylesheet_dir_uri, $stylesheet, $theme_root_uri) {

return str_replace(home_url(), CDN_HOST, $stylesheet_dir_uri);

}

add_filter(template_directory_uri,z_cdn_template_directory_uri,10,3);

function z_cdn_template_directory_uri($template_dir_uri, $template, $theme_root_uri)

{

return str_replace(home_url(), CDN_HOST, $template_dir_uri);

}

将 wp-content / wp-includes 静态资源替换为CDN地址:

define(FocusCDNHost,http://www.softhome.cc);//wordpress网站网址

define(FocusCDNRemote,CDN网址);//cdn域名

define(FocusCDNIncludes,wp-content,wp-includes);//设置加速目录

define(FocusCDNExcludes,.php|.xml|.html|.po|.mo);//设置文件白名单

define(FocusCDNRelative,);//Check this if you want to have links like wp-content/abc.png rewritten - i.e. without your blogs 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);

}

}

如果您并没有使用前面的将本地图片地址替换为CDN地址代码,还需补充一条:

define(CDN_HOST,CDN网址);

*特别声明:资源收集自网络或用户上传,本网站所提供的电子文本仅供参考,请以正式出版物为准。电子文本仅供个人标准化学习、研究使用,不得复制、发行、汇编、翻译或网络传播等。如有侵权,请联系我们处理。

相关