You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将WordPress中的CDN脚本替换为本地服务器副本?

Replacing External CDN Resources with Local Copies in WordPress

Absolutely! Swapping those slow external CDN resources for local copies is totally feasible, and it’ll make a huge difference in your site’s load speed for visitors in China. Let me walk you through exactly how to do this step by step:

1. Download the CDN Resources to Your Local Server

First, grab the files you want to replace:

  • Visit the CDN URLs you mentioned (like https://ajax.googleapis.com/ajax/libs/webfont/1.5.0/webfont.js and https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css) in your browser, then save each file to your computer.
  • For resources like jQuery UI, don’t forget to download associated assets (like icon fonts) that the CSS file references—you can find these by checking the CSS code for @font-face rules.
  • Upload all these files to your WordPress theme folder (I recommend creating subfolders like assets/js for scripts and assets/css for stylesheets to keep things organized). You can do this via FTP or your hosting control panel’s file manager.

2. Replace References in Your Theme

Most well-coded themes use WordPress’s official enqueue functions to load assets. Here’s how to update them:

  • Open your theme’s functions.php file (if you’re using a child theme, use the child theme’s functions.php to avoid losing changes when the parent theme updates).
  • Look for lines that use wp_enqueue_script() or wp_enqueue_style() pointing to the external CDN URLs. For example:
    wp_enqueue_script( 'webfont', 'https://ajax.googleapis.com/ajax/libs/webfont/1.5.0/webfont.js', array(), '1.5.0', true );
    wp_enqueue_style( 'jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css', array(), '1.9.0' );
    
  • Replace the CDN URL with the local file path using get_template_directory_uri() (for parent themes) or get_stylesheet_directory_uri() (for child themes):
    wp_enqueue_script( 'webfont', get_template_directory_uri() . '/assets/js/webfont.js', array(), '1.5.0', true );
    wp_enqueue_style( 'jquery-ui', get_template_directory_uri() . '/assets/css/jquery-ui.css', array(), '1.9.0' );
    
  • If your theme uses raw <script> or <link> tags in template files (like header.php), update those URLs directly to the local path using the same helper functions:
    <script src="<?php echo get_template_directory_uri(); ?>/assets/js/webfont.js"></script>
    <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/assets/css/jquery-ui.css">
    

3. Handle Plugin-Based CDN Resources

Modifying plugin files directly is risky—updates will overwrite your changes. Instead, use WordPress hooks to deregister the external resources and re-register local versions:

  • Add this code to your theme’s functions.php file:
    function replace_external_cdn_assets() {
        // Deregister the original CDN resources
        wp_deregister_script( 'webfont' );
        wp_deregister_style( 'jquery-ui' );
        
        // Register your local versions
        wp_register_script( 'webfont', get_template_directory_uri() . '/assets/js/webfont.js', array(), '1.5.0', true );
        wp_register_style( 'jquery-ui', get_template_directory_uri() . '/assets/css/jquery-ui.css', array(), '1.9.0' );
        
        // Enqueue the local assets
        wp_enqueue_script( 'webfont' );
        wp_enqueue_style( 'jquery-ui' );
    }
    add_action( 'wp_enqueue_scripts', 'replace_external_cdn_assets', 99 );
    
  • The 99 priority ensures your code runs after the plugin enqueues its assets, so you can successfully deregister the original files. Note: You need to use the exact "handle" name the plugin uses (like webfont or jquery-ui)—check the plugin’s code or your browser’s developer tools (look at the resource’s ID) to confirm this.

Important Notes

  • Check for dependent assets: For resources like jQuery UI, make sure all linked files (like fonts) are in the correct relative path, or update the CSS file to use absolute local URLs for these assets.
  • Keep assets updated: While local loading is faster, remember to periodically check for security or feature updates to the original resources and replace your local copies if needed.
  • Test thoroughly: After making changes, clear your site’s cache and test all functionality to ensure nothing breaks (like interactive elements relying on jQuery UI).

内容的提问来源于stack exchange,提问作者Li Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:51:45