如何将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.jsandhttps://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-facerules. - Upload all these files to your WordPress theme folder (I recommend creating subfolders like
assets/jsfor scripts andassets/cssfor 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.phpfile (if you’re using a child theme, use the child theme’sfunctions.phpto avoid losing changes when the parent theme updates). - Look for lines that use
wp_enqueue_script()orwp_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) orget_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 (likeheader.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.phpfile: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
99priority 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 (likewebfontorjquery-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
相关产品推荐
相关产品推荐

