$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WooCommerce or any other plugin: Deliver JS and CSS through CDN without using a Plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far