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

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.com/wp-content/plugins/woocommerce/assets/js/jquery-blockui/jquery.blockUI.min.js
  • example.com/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js'
  • example.com/wp-content/plugins/woocommerce/assets/js/js-cookie/js.cookie.min.js'
  • example.com/wp-content/plugins/woocommerce/assets/js/frontend/woocommerce.min.js
  • example.com/wp-content/plugins/woocommerce/assets/js/jquery-cookie/jquery.cookie.min.js
  • example.com/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.

Topic woocommerce-offtopic plugins-url plugins Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.