搜索
Hi~登录注册
查看: 1441|回复: 0

wordpress加速优化教程:非插件部署cdn加速(七牛、又拍云) |wordpress教程

[复制链接]

1892

主题

1899

帖子

6406

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6406
发表于 2015-4-15 14:54:27 | 显示全部楼层 |阅读模式
对于cdn加速相信各位wordpress博主一定不陌生,毕竟网站加速是每个站长都关注的事情,而使用cdn加速静态资源对于网站的加速优化又占到了很大的比重。国内比较知名的两家cdn厂商七牛和又拍云都有对应的wordpress插件,不过使用插件本身就会影响到wordpress的运行速度和效率,虽然影响的速度很小几乎可以忽略不急但是对于小V这种强迫症患者是不允许任何一点速度上的浪费,但是如果不用cdn加速插件那么手动上传文件又大大的降低了小V更新文章的效率,那么只有将cdn插件的代码集成到主题中了,代码如下(加到functions.php文件即可): define('FocusCDNHost','http://www.v7v3.com');//wordpress网站网址define('FocusCDNRemote','http://0.v7v3.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 <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);} } 以上代码中四个参数可以根据你自己的需求来自定义,其中网站域名和cdn域名是必须替换成你自己的(PS:cdn域名可以是七牛的也可以是又拍云的,不过使用时需要开启镜像功能,镜像网站为你自己的网站。)设置加速目录,这个预设好了,一般情况下不需要修改。文件白名单则是指不缓存的特定文件。  代码参考:kn007.net
公众微信:idc5ahl
公众QQ:吾爱互联
关注公众微信,公众QQ每天领现金卡密
卡密介绍(http://www.5ahl.com/thread-2182-1-1.html
回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 点我注册

快速回复 返回顶部 返回列表