I try to go one step forward based on this question and instead of overriding the whole .js file of another plugin, i'm wondering is it possible to override/swap only a part of that js file dynamically?

To somehow maybe 'inject'/ import my own function(s) additionally into that specific file safely (and fairly natively:)?

1 Answer 1


Unfortunately no, you can dequeue the file as the other questions answer suggests and enqueue your own version, otherwise you'd need to load it and then try to modify the loaded code at runtime, which is almost always not feasible.

Monkeypatching might be a solution, assuming your code runs before theirs does, but is loaded after there's. But that is rarely possible. We're talking 1 in a thousand chance. This is where the original code is loaded, but before it's ran, your code swaps out functions and data structures. e.g. removing a jQuery plugin then putting your own in its place. This isn't possible in most situations though. Note that monkeypatching is widely considered a kludge, is not a longterm solution, and can cause issues on its own.

It's also possible that the author of the original file wanted their code to be extensible and built it that way. But this is unlikely. You would need to read the code to find out on a case by case basis.

Eitherway there is no WP based solution for this. To continue down this avenue you will need to ask on stackoverflow, specifically how to override functionality in a javascript file that's already been loaded using a newly loaded javascript file. For most situations the answer to this is that you cannot. For those rare situations where you can force it, you'll need advanced javascript knowledge.

  • Thanks Tom! Does that mean, that we are generally 'confined' to manual tracking and updating on these files (if we decide to dequeue-enqueue them) in most cases? For example even in a native woocommerce asset/js file: "add-to-cart-variation.js" i can't yet find signs of it being pluggable on function-level. Cheers, Jun 29, 2020 at 15:46
  • Pluggable functions like that are very rare. So yes, you'll need to dequeue and enqueue, then check on updates.
    – Tom J Nowell
    Jun 29, 2020 at 19:36
  • 1
    Hm, is that only me who finds it a bit odd? As i see how well pluggable functions are working in let's say Wordpress ecosystem. I wonder why isn't there a 'culture' for this in the js world either. Maybe i just don't know javascript well enough yet to understand the constraints/drawbacks of this(?).. Jul 1, 2020 at 8:19
  • Pluggable functions are more of a legacy method of extending WP from the oldest days, it's klunky and problematic ( e.g. you can only override once ). It was replaced for the most part with filters and hooks, nobody will tell you to redeclare wp_mail in a dropin too get custom HTML emails these days. Pluggable functions outside of the original WP 2 provided are very rare in modern WP PHP. As for the JS world, JS gets loaded differently from PHP, and it's just not as convenient. On top of that, that's just not how JS dev works, your situation is unusual and forced by WP theming
    – Tom J Nowell
    Jul 1, 2020 at 8:46
  • Normally you would fork the JS, and rely on libraries and shared packages pulled in via npm, or loaded from elsewhere like jQuery plugins. Being able to override templates in a chiild theme is an example of the exception not the rule. It's a special case system specifically built for theme template files loaded via get_template_part, it's not a widespread programming paradigm at the language level
    – Tom J Nowell
    Jul 1, 2020 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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