wking's blog

  • 文章分类
    • 日常随笔
    • IT技术
    • 系统封装
    • 大航海时代
  • 关于博主
GOD'S IN HIS HEAVEN, ALL'S RIGHT WITH THE WORLD.
  1. 首页
  2. IT技术
  3. 正文

WordPress添加代码实现自动替换媒体URL为CDN URL

2024-02-24 540点热度 0人点赞 0条评论

WordPress添加代码实现自动替换媒体URL为CDN URL。这个方法应该是WordPress CDN加速中最简单又好用的图片CDN方法了。

这个方法的好处是媒体资源仍然保存在博客服务器,方便整站备份、迁移,同时只要CDN厂商支持从源站(也就是博客)获取资源功能,仍然可以轻松CDN媒体资源。前台文章里的图片资源直接从CDN读取,减轻服务器压力,加快网页速度。配合Autoptimize插件的整合JS/CSS并开启CDN功能,可大幅提升博客访问速度。

本方法具体操作是在 WordPress 主题目录下的 functions.php 文件中,加入以下代码实现替换功能(推荐使用WPCode Lite插件实现轻松插入和代码段管理功能)。

有两个版本的代码,简单和复杂的。

简单版本

只能修改URL带有指定目录的路径,全改为CDN URL。不影响网站内其他相对链接。

PHP
1
2
3
4
5
6
7
8
9
define('CDN_HOST','//cdn.blog.com');  // 修改为CDN域名
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); // 要替换的目录URL
}
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);
}

如果你想要将主题的css、图片以及js等静态资源替换为CDN 的url进行加速,那么可以使用以下代码:

PHP
1
2
3
4
5
6
7
8
9
10
define('CDN_HOST','http://你的CDN链接');
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);
}

原文:https://www.themepark.com.cn/wordpresssycdnjsthurl.html

复杂版本

网页内的所有相对链接都会被替换。

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//静态文件CDN加速
if ( !is_admin() ) {
    add_action('wp_loaded','cdn_start');
    
    function cdn_start() {
        ob_start('cdn_replace');
    }    
function cdn_replace($html){
    $local_host = 'blog.com'; //博客域名
    $cdn_host = 'cdn.blog.com'; //CDN域名
    $cdn_exts   = 'css|js|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, $cdn_host . '$1$4', $html);
    } else {
        $regex    = '/' . str_replace('/', '\/', $local_host) . '\/([^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/';
        $html =  preg_replace($regex, $cdn_host . '$1$3', $html);
    }
    return $html;
}
}

原文 https://cloud.tencent.com/developer/article/1150832?areaSource=106002.19

本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
标签: wordpress
最后更新:2024-03-12

wking

不管博客型博主

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

目录
  • 简单版本
  • 复杂版本
标签聚合
大航海时代 wordpress 一支红杏 C++ win10 linux R6300V2 OneNote

COPYRIGHT © 2024 wkings.blog. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang