wordpress无需插件纯代码实现将网站静态资源(css,js)存储到又拍云、七牛、腾讯云、阿里云等对象存储中给网站加速-凯发网娱乐官网

微信扫一扫,分享到朋友圈

0

wordpress七牛镜存储插件,wp super cache的cdn功能,都是可以通过代码实现的。

第一步

把你博客里的wp-contentwp-includes目录上传到七牛或又拍云、腾讯云、阿里云的对象存储中

 

第二步

上传文件完之后,将以下代码中的网站地址修改成你的博客域名(注:结尾不要加 / )cdn域名修改成对象存储绑定的域名没有就填默认的域名(注:结尾不要加 / ),然后将以下代码放入主题的functions.php文件中最底部即可

define('focuscdnhost','http://www.imotao.com');//wordpress网站网址
define('focuscdnremote','http://cdn.imotao.com');//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  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);
}
}

 

展开阅读全文
这个博主很懒,懒死了。

wordpress评论防止游客冒充站长回复别人

wordpress评论回复添加验证码防止垃圾评论

你也可能喜欢

发表评论

this site uses akismet to reduce spam. .

插入图片