WooCommerce or any other plugin: Deliver JS and CSS through CDN without using a Plugin

admin2025-06-03  4

I have a straightforward question:

How do I change the links (base URL) of the JS and CSS assets provided by WooCommerce (or any other plugin) in order to use a CDN?

The only requirements I have:

  • I do not want to use any plugin.
  • I do not want to change any code in wp-content/plugins as this will be overwritten by the next updates.
  • I do not want to change WP_PLUGIN_DIR or WP_PLUGIN_URL, because other plugins and plugin components rely on this path and I don't want / can not upload the whole plugin code to the CDN (correct me please if I'm wrong in any way).

To be precise, I'm talking about the following assets:

  • example/wp-content/plugins/woocommerce/assets/js/jquery-blockui/jquery.blockUI.min.js
  • example/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js'
  • example/wp-content/plugins/woocommerce/assets/js/js-cookie/js.cookie.min.js'
  • example/wp-content/plugins/woocommerce/assets/js/frontend/woocommerce.min.js
  • example/wp-content/plugins/woocommerce/assets/js/jquery-cookie/jquery.cookie.min.js
  • example/wp-content/plugins/woo-poly-integration/public/js/Cart.min.js

I checked the plugin code and they register the assets like this:

wp_register_script( 'jquery-blockui', WC()->plugin_url() . 
'/assets/js/jquery-blockui/jquery.blockUI' . $suffix .
'.js', array( 'jquery' ), '2.70', true );

Sure, I could change the plugin_url() function, but that would have two disadvantages: First other components rely on the plugin_url (I would brake other functionality) and second, I would loose the changes with every new update.

Any help would be appreciated.

I have a straightforward question:

How do I change the links (base URL) of the JS and CSS assets provided by WooCommerce (or any other plugin) in order to use a CDN?

The only requirements I have:

  • I do not want to use any plugin.
  • I do not want to change any code in wp-content/plugins as this will be overwritten by the next updates.
  • I do not want to change WP_PLUGIN_DIR or WP_PLUGIN_URL, because other plugins and plugin components rely on this path and I don't want / can not upload the whole plugin code to the CDN (correct me please if I'm wrong in any way).

To be precise, I'm talking about the following assets:

  • example/wp-content/plugins/woocommerce/assets/js/jquery-blockui/jquery.blockUI.min.js
  • example/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js'
  • example/wp-content/plugins/woocommerce/assets/js/js-cookie/js.cookie.min.js'
  • example/wp-content/plugins/woocommerce/assets/js/frontend/woocommerce.min.js
  • example/wp-content/plugins/woocommerce/assets/js/jquery-cookie/jquery.cookie.min.js
  • example/wp-content/plugins/woo-poly-integration/public/js/Cart.min.js

I checked the plugin code and they register the assets like this:

wp_register_script( 'jquery-blockui', WC()->plugin_url() . 
'/assets/js/jquery-blockui/jquery.blockUI' . $suffix .
'.js', array( 'jquery' ), '2.70', true );

Sure, I could change the plugin_url() function, but that would have two disadvantages: First other components rely on the plugin_url (I would brake other functionality) and second, I would loose the changes with every new update.

Any help would be appreciated.

Share Improve this question asked Feb 11, 2019 at 19:39 manifestormanifestor 3136 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

WC()->plugin_url() is basically a wrapper for the core function plugins_url(). That functions return value is filtered. return apply_filters( 'plugins_url', $url, $path, $plugin ); So you should be able to add a filter to that hook that returns the initial value if $plugin isn't woocommerce, then checks if the path or URL leads to one of the scripts you're trying to replace and sends the appropriate value on from there.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748917953a314804.html

最新回复(0)