
在 WordPress 主题目录下的 functions.php 文件中,加入以下代码实现替换功能:
//静态文件CDN加速
if ( !is_admin() ) {
add_action('wp_loaded','miaonav_ob_start');
function miaonav_ob_start() {
ob_start('miaonav_qiniu_cdn_replace');
}
function miaonav_qiniu_cdn_replace($html){
$local_host = 'https://miaonav.com'; //博客域名
$qiniu_host = 'xxxxxxxxx'; //CDN域名
$cdn_exts = '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;
}
}
以上。
暂无评论...