Why are we using anything like wp_enqueue_script at all? or register, etc. Why are we sending Javascript through the PHP subsystem? There are all kinds of incompatibilities and inconsistencies across front-end systems, and these are hard enough to troubleshoot without WP getting in the way. So why are we mixing the controller with the view? What's the model, here?

  • 2
    wp_enqueue_script isn't sending javascript through php, it's simply managing the insertion of script tags on the front end. – Milo Dec 18 '14 at 1:08
  • Point taken; this is a issue of wording on my part – McJimbo Dec 18 '14 at 12:51

The primary reason we use enqueue scrips is because WordPress will properly manage all the requests. For example, if two different plugins are using a certain third party library, WordPress will make sure that the script is only loaded once. It will also do a good job of handeling multiple version of the same script.

Lastly, you will also be able to conditionally load scripts. i.e. admin page scripts can be set to only load when inside the admin.

If you import your scripts and bypass this feature, you can no longer take advantage of this ability of WordPress to optimize the loading of scripts by multiple authors.

I hope that helps.

| improve this answer | |
  • Right, that does make sense. Seems like it would work well if you need, it, though, and make a terrible mess if you don't. I see sites & plugins noting incompatibilities between WP & js scripts and across scripts when used w/WP all over the place. Is it often enough that it's really warranted to rely upon the CMS to manage front-end scripting? I just wonder if folks at WP and the devs that employ it every day have weighed this issue with the problems thus evident. (...or apparent. :) Your answer does clarify the motivation, either way. Thanks! – McJimbo Dec 18 '14 at 12:58
  • 1
    @McJimbo can you cite some examples of these incompatibilities "all over the place" that are related to the enqueue system? In 6 years of developing with WordPress, the only issues I encounter stem from not using the API, or using it incorrectly. What do you suggest would be a better system for managing dependencies and load order between core, theme, and plugin scripts? of course, you are entirely free to not use the API, but I highly doubt you will simplify any aspect of your development in doing so. – Milo Dec 18 '14 at 16:53

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