最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - "Intercept" enqueing of 3rd party's JS file

programmeradmin3浏览0评论

I'm using a 3rd-party plugin, but I need to load the non-minified version of a specific JS file of the plugin, to avoid some image preloading caching; in the AJAX call when preloading some images, the minified JS uses param cache:!0 that breaks a specific functionality that I need...

So, the relevant code in the plugin reads:

        wp_enqueue_script( 'woo-variation-gallery', esc_url( $this->assets_uri( "/js/frontend{$suffix}.js" ) ), array(
            'jquery',
            'wp-util',
            'woo-variation-gallery-slider',
            'imagesloaded',
            'wc-add-to-cart-variation'
        ), $this->version(), true );

where $suffix is calculated a little above (that's pretty standard in every plugin basically) like this:

        $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

So, temporarily I got around this by setting SCRIPT_DEBUG to true in my wp-config.php, but it doesn't feel right loading non-minified scripts site-wide just for a single script that needs to be loaded in its non-minified version...

So, is there any hook involved while enqueuing scripts, to hook into in my functions.php file in order to replace the script's $src from the plugin's /js/frontend.min.js to /js/frontend.min.js before loading it?

I'm using a 3rd-party plugin, but I need to load the non-minified version of a specific JS file of the plugin, to avoid some image preloading caching; in the AJAX call when preloading some images, the minified JS uses param cache:!0 that breaks a specific functionality that I need...

So, the relevant code in the plugin reads:

        wp_enqueue_script( 'woo-variation-gallery', esc_url( $this->assets_uri( "/js/frontend{$suffix}.js" ) ), array(
            'jquery',
            'wp-util',
            'woo-variation-gallery-slider',
            'imagesloaded',
            'wc-add-to-cart-variation'
        ), $this->version(), true );

where $suffix is calculated a little above (that's pretty standard in every plugin basically) like this:

        $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

So, temporarily I got around this by setting SCRIPT_DEBUG to true in my wp-config.php, but it doesn't feel right loading non-minified scripts site-wide just for a single script that needs to be loaded in its non-minified version...

So, is there any hook involved while enqueuing scripts, to hook into in my functions.php file in order to replace the script's $src from the plugin's /js/frontend.min.js to /js/frontend.min.js before loading it?

Share Improve this question edited Jan 9, 2022 at 3:17 Faye D. asked Jan 9, 2022 at 3:07 Faye D.Faye D. 1266 bronze badges 12
  • 1 I'm guessing that you've made manual modifications to the file but don't know how to make the same modification to the minified versions to workaround what the plugin does? Would it not be easier to dequeue and deregister the script then re-register and re-enqueue it but with your own copy instead? Instead of trying to fix a hole in your cars tire while it's still driving at 60mph, why not stop it and replace the tire with a new one instead? This Q is an X Y problem](xyproblem.info), there are much easier ways to solve your problem than what you've asked about. – Tom J Nowell Commented Jan 9, 2022 at 3:26
  • @TomJNowell no, I haven't made any direct changes to the non-minified version of the JS file... If that was the case, I'd use a JS minifier to minify its code... Indeed I've written my own code to extend its functionality in a way I needed (wrote some jQuery code utilizing an event handler of the plugin - on('hide_variation.wvg', function() {)... – Faye D. Commented Jan 9, 2022 at 3:34
  • and have you raised this with their support route? – Tom J Nowell Commented Jan 9, 2022 at 3:36
  • Of course, but it being a freemium plugin, I assume they reply only to the paid customers, while ignoring their free ones... – Faye D. Commented Jan 9, 2022 at 3:38
  • 1 there's no accusation or malign intent, the x y problem is a common mistake and a fundamental part of how humans work, made by everybody regardless of seniority/role/industry/purpose. It's not something you grow out of, it's something everybody has to be actively mindful of that creeps into everything. Nobody said this makes you a mouse developer, or that only inexperienced people do it, everybody does it even me – Tom J Nowell Commented Jan 9, 2022 at 17:47
 |  Show 7 more comments

1 Answer 1

Reset to default 0

I was able to deal with this by letting the minified JS load normally, and then de-registering and de-queuing it, and finally enqueuing the non-minified one... Although personally I'd consider this an indirect method, it's probably the only way to do it, since there is obviously no way to hook into the process of originally enqueuing a script...

So my solution was inspired by a comment to my question by @TomJNowell and by this answer to another question also!

So the code I used was this:

add_action('wp_enqueue_scripts', 'wvg_load_non_minified_js', 10, 0);
function wvg_load_non_minified_js()
{
    wp_deregister_script('woo-variation-gallery');
    wp_dequeue_script('woo-variation-gallery');

    wp_enqueue_script('woo-variation-gallery', esc_url(plugins_url() . '/woo-variation-gallery/assets/js/frontend.js'), array(
        'jquery',
        'wp-util',
        'woo-variation-gallery-slider',
        'imagesloaded',
        'wc-add-to-cart-variation',
    ), WOO_VG_VERSION, true);
}
发布评论

评论列表(0)

  1. 暂无评论