想给网站的地址换个cdn静态地址,但是找了半天WordPress插件都太复杂了,于是自己写了一个
在/wp-includes/functions.php文件末尾,加上
//hack for replace cdn site url
function replace_cdn_url_callback( $buffer ) {
$blog_url = 'https://kimi.pub';
$cnd_url = 'https://static.kimi.pub';
$include_dirs[] = 'wp-content';
$include_dirs[] = 'wp-includes';
$include_dirs[] = 'download';
$arr_dirs = array();
foreach ( $include_dirs as $dir ) {
$arr_dirs[] = preg_quote( trim( $dir ), '`' );
}
$dirs = implode( '|', $arr_dirs );
$regex = '`(?<=[(\"\'])' . preg_quote( $blog_url, '`' ) . '/(?:((?:' . $dirs . ')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])`';
return preg_replace_callback( $regex, 'replace_cdn_url_rewriter', $buffer );
}
function replace_cdn_url_rewriter( $match ) {
$blog_url = 'https://kimi.pub';
$cnd_url = 'https://static.kimi.pub';
if( count($match) == 2 ) {
return str_replace( $blog_url, $cnd_url, $match[0]);
} else {
return $match[0];
}
}
function cdn_url_buffer_start() {
ob_start("replace_cdn_url_callback");
}
function cdn_url_buffer_end() {
ob_end_flush();
}
add_action('init', 'cdn_url_buffer_start');
add_action('shutdown', 'cdn_url_buffer_end');
文章中的 https://kimi.pub https://static.kimi.pub 记得替换成自己的网址,同时每次升级都需要重新添加一下