前几天有人问我是如何把https://www.ljy2345.com/wp-content/plugins/wp-minify/cache/677d0f0eb4cc0c4b807a8a5b838148bb.css?m=1443052900替换成http://static.ljy2345.com/wp-content/plugins/wp-minify/cache/677d0f0eb4cc0c4b807a8a5b838148bb.css?m=1443052900的。其实就是利用七牛云存储和WP-Super-Cache插件实现的。
设置镜像源
就是你的网站地址,,,
设置域名
如果你的域名已经备案,那么就可以设置二级域名作为访问静态存储文件的地址。在七牛工作人员审核后即可启用。(使用方法:在DNS解析中,将设置的二级域名CNAME 到你现在绑定的那个 xxxx.ljy2345.com 二级域名就可以了。)
没有备案只能用七牛源码,区别就在于美观性。
配置插件
打开WP-Super-Cache插件设置--CDN。http://static.ljy2345.com 设置为你的七牛域名,然后勾选--保存,,,
非插件方法
如果你不喜欢插件,可以使用下面的代码方法
在 WordPress 主题目录下的 functions.php
文件中,加入以下代码实现替换功能:
if ( !is_admin() ) { add_action('wp_loaded','ljy2345_ob_start'); function ljy2345_ob_start() { ob_start('ljy2345_qiniu_cdn_replace'); } function ljy2345_qiniu_cdn_replace($html) { return str_replace('[这里填写博客路径]', '[这里填写七牛空间路径]', $html); } }
以我的设置上传目录 wp-content/uploads/
文件夹为例,路径就应该替换为如下代码:
return str_replace('https://www.ljy2345.com/wp-content/uploads/', 'http://static.ljy2345.com/wp-content/wp-uploads/', $html);
如果你需要更具体的范围控制(多目录、指定文件类型),可使用以下代码替换上面的ljy2345_qiniu_cdn_replace
部分:
function ljy2345_qiniu_cdn_replace($html){ $local_host = 'https://www.ljy2345.com/'; //博客域名 $qiniu_host = 'http://static.ljy2345.com/'; //七牛域名 $cdn_exts = 'js|css|png|jpg|jpeg|gif|ico'; //扩展名(使用|分隔) $cdn_dirs = 'wp-content|wp-includes'; //目录(使用|分隔) $cdn_dirs = str_replace('-', '\-', $cdn_dirs); if ($cdn_dirs) { $regex = '/' . str_replace('/', '\/', $local_host) . '\/((' . $cdn_dirs . ')\/[^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/'; $html = preg_replace($regex, $qiniu_host . '/$1$4', $html); } else { $regex = '/' . str_replace('/', '\/', $local_host) . '\/([^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/'; $html = preg_replace($regex, $qiniu_host . '/$1$3', $html); } return $html; }
:mrgreen:
赞赏除特别注明外,本站文章均采用BY-NC-SA协议授权,转载请注明来自:https://www.ljy2345.com/2015/09/%e6%8f%92%e4%bb%b6%e5%ae%9e%e7%8e%b0wordpress%e5%8d%9a%e5%ae%a2%e9%9d%99%e6%80%81%e6%96%87%e4%bb%b6cdn%e5%8a%a0%e9%80%9f/
Comments | NOTHING