将静态资源移植到对象存储不仅可以提升网站加载速度,还可以大大的降低服务器的负载,但让人头痛的往往是将静态资源放入对象存储后需要修改本地源文件中的静态资源链接,大量的静态链接需要从源代码中一行行查询修改,不仅工作量大搞不好还容易出错一个没搞好到头来所有的操作都是白费力气,这个时候虽然可以利用WordPress插件解决问题而作为强迫症的博主却十分不喜这种操作因为插件安装过多反而会造成网站速度缓慢,不多说我们直接上教程。
在主题functions.php文件中添加如下代码:
define('CDN_HOST','//cdn.static.bianh.cn');//注意替换为自己的地址
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);
}
注意其中第一行中的链接需要替换为你自己的地址
需要将图片地址也替换的话继续添加如下代码:
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);
}//如果有修改过wordpress的附件地址此处也要修改
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);
}
本文来源于:WordPress非插件替换静态文件链接-变化吧门户
特别声明:以上文章内容仅代表作者本人观点,不代表变化吧门户观点或立场。如有关于作品内容、版权或其它问题请于作品发表后的30日内与变化吧联系。
- 赞助本站
- 微信扫一扫
-
- 加入Q群
- QQ扫一扫
-
评论