目前在wordpress网站维护上,有不少的朋友选择将网站的静态资源,如图片、css、js等放入CDN进行加速处理,那么如果我们已经设置好了cdn的一些数据,并已经同步好了本地数据,那么我们如何调用cdn的数据呢?
一般来说,我们使用集中方式来调用,一种是使用插件,比如七牛就有专门适配的wordpress插件进行操作,如果你在使用静态化插件WP Super Cache ,那么你也可以使用这个插件的CDN功能对url进行替换,从而达到加速的效果。
如果你不想要使用插件,那么下面我们就来介绍一些代码,教你如何对网站的本地资源url替换为CDN资源的url。
代码使用方法
只要将代码放入主题的function.php中即可实现了
使用代码将wordpress本地资源替换为CND资源
将文章附件图片url替换为cdn资源,这里指的是,文章内插入的本地图片、本地附件以及设置的特色图片,加入以下代码之后,所有url均会更改为你的CNDurl:
define('CDN_HOST','https://cdn.ycb.cc');
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’,’https://cdn.ycb.cc’); 需要替换为你自己的cdn地址。这个cdn链接将会替换你的本地url。
加入上面的代码之后,你插入文章内的图片和其他附件以及特色图片都会被替换为cdn的url了,这样也就达到了加速的效果。
如果你想要将主题的css、图片以及js等静态资源替换为CDN 的url进行加速,那么可以使用以下代码:
define('CDN_HOST','https://cdn.ycb.cc');
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);
}
以上两种代码可以组合使用,如果组合使用的话,记得删掉第二个代码中的define('CDN_HOST','https://cdn.ycb.cc');
这句,因为第一个代码中也包含了这句,如果组合使用这句就重复了,只需要保留一个即可,这样你就可以实现文章内插入的本地图片、本地附件以及设置的特色图片,还有主题的css、图片以及js等静态资源更改为使用CDN地址了。
这里再提供一个加强型的代码,可以设置指定目录替换CDN地址以及设置指定文件格式为白名单不替换CDN地址,大家可以根据需求来更改
代码如下
define('FocusCDNHost','https://ycb.cc');//wordpress网站网址
define('FocusCDNRemote','https://cdn.ycb.cc');//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 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);
}
}
之前也发过网站动静分离的方案文章,大家可以参考一下