WordPress非插件实现静态资源CDN功能
wordpress吧
全部回复
仅看楼主
level 14
  之前发布了极限优化WordPress!打造完美站点:https://tieba.baidu.com/p/3050218986 此文,其中的CDN功能可以使用WP Super Cache实现,本帖将通过添加代码的方式,将静态资源地址全部更换为另外一个地址,一劳永逸。
  假若我的CDN地址是cdn.mywpku.com,那么位于www.mywpku.com上的所有数据将会原封不动地保存在我的CDN上面,这是前提条件。
2014年05月18日 00点05分 1
level 14
将本地图片地址替换为CDN地址
添加至主题目录functions.php中:
define('CDN_HOST','http://cdn.mywpku.com');
add_filter('the_content','z_cdn_content');
function z_cdn_content($content){
return str_replace(home_url().'/wp-content/uploads', CDN_HOST.'/wp-content/uploads', $content);
}
add_filter('wp_get_attachment_url','z_get_attachment_url',10,2);
function z_get_attachment_url($url, $post_id){
return str_replace(home_url(), CDN_HOST, $url);
}
注意 define('CDN_HOST','http://cdn.mywpku.com'); 需要替换为你自己的CDN地址。
2014年05月18日 00点05分 2
level 14
将主题静态资源地址替换为CDN地址
添加至主题目录functions.php中:
add_filter('stylesheet_directory_uri','z_cdn_stylesheet_directory_uri',10,3);
function z_cdn_stylesheet_directory_uri($stylesheet_dir_uri, $stylesheet, $theme_root_uri) {
return str_replace(home_url(), CDN_HOST, $stylesheet_dir_uri);
}
add_filter('template_directory_uri','z_cdn_template_directory_uri',10,3);
function z_cdn_template_directory_uri($template_dir_uri, $template, $theme_root_uri)
{
return str_replace(home_url(), CDN_HOST, $template_dir_uri);
}
如果您并没有使用前面的“将本地图片地址替换为CDN地址”代码,还需补充一条:
define('CDN_HOST','http://cdn.mywpku.com');
2014年05月18日 00点05分 3
level 11
前排抱大腿
2014年05月18日 00点05分 4
level 14
。。。
2014年05月18日 01点05分 5
1