当你想用CDN而又不想安装插件时。可以用该代码
把该代码插入wordpress functions.php 里中间位置即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
//免插件七牛CDN加速 define('FocusCDNHost','http://www.mingshao.net');//网站地址 define('FocusCDNRemote','http://cdn.mingshao.net');//cdn地址 define('FocusCDNIncludes','wp-content,wp-includes');//cdn加速目录 define('FocusCDNExcludes','.php|.xml|.html');//不缓存文件后缀 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); } } //免插件七牛CDN加速 |
七牛支持中文域名了没?
不清楚,你去试试?或直接咨询客服。
嗯
加到functions.php里面不就等于是一个插件吗?效果差不多,资源占用也没有好多少。
不如直接将静态资源放到7牛