0

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.

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.

  • But in this case I would change the plugin url for each component of the plugin and then I would brake other functionality in the plugin, or am I wrong with that assumption? – chevallier Feb 11 at 20:36
  • If you need it to be that specific then you could use a regex to check if the $url or $path variable was specifically for one of the scripts you're looking to change and only make changes if it is. – mrben522 Feb 12 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.