I'm starting to refactor the code of my plugin and I'm thinking about using require.js to handle the scripts so that i have a codebase which is more modular and testable. And I'm also planning to load by myself all the required dependencies (jQuery included) so that I'm not hindered by WordPress versions.

Has anyone ever done anything similar?

  • 1
    Just a hint: Link to source.
    – kaiser
    Jun 5, 2012 at 21:13
  • 2
    Can you detail what sorts of scripts you would be loading like this? Offhand I would say it was a really bad idea because if you don't work with the WP system of registering and enqueueing, you'll likely end up conflicting with other plugins; loading multiple versions of jQuery on a page, and all the other problems that the wordpress scripts API was supposed to solve. Jun 5, 2012 at 22:15
  • @goldenapples requirejs should handle multiple version of jQuery with a few lines of code leaving the orginal jQuery intact. After that there should be nothing to worry about as everything i loaded into anonymous function and i'm loading everything i need, and of course i would rely on wordpress api only to load the main.js file. But i was asking if anyone else did it because maybe this is a terrible idea Jun 5, 2012 at 23:31

2 Answers 2


Yes I'm doing something similar: I'm building a plugin and I'm loading some plugin specific Javascript via Modernizr.load. It's fairly much Javascript and I don't want to slow down page load times. I currently don't intend to load jQuery in this manner though.

Assume that I did load jQuery via Modernizr. Then what happens if someone activates both your plugin, and my plugin? Might we then load jQuery, twice? (Perhaps we'd load jQuery 1.7.2 once via require.js and once via Modernizr? I don't know how these script loaders work, internally.)

I guess that if you load only stuff that you really really know is specific to your plugin only, then you'll be fine.

(Danger: This is my first WordPress plugin and I don't know WordPress or PHP well.)

  • 1
    I'me almost done with the refactoring and i'm quite happy about it. I managed to load jQuery and do not touch wordpress version, in this way my plugin use my version of jQuery and it's totally indipendent from the wordpress version it's running on. Jun 22, 2012 at 14:47
  • Just to know, how do you handle the possibility that another plugin might use requirejs? Jun 29, 2012 at 15:57
  • @NicolaPeluchetti As of now, I don't :-(
    – KajMagnus
    Jun 29, 2012 at 19:18
  • I found some resources, there are some options available, it depends on how you are structuring your app, take a look here requirejs.org/docs/faq-advanced.html#rename Jun 29, 2012 at 19:21
  • @NicolaPeluchetti I don't quite understand — why would I want to rename Requirejs? I'm using Modernizr. Well perhaps if some other plugin uses another non-compatible version of Modernizr then I might want to load my own version of Modernizr, was that what you meant? Thanks anyway for the suggestion :-)
    – KajMagnus
    Jun 29, 2012 at 19:31

I've had to do a very similar thing to what you're asking, so I put together a Github repository of a Wordpress plugin template using RequireJS.

Once you get the basic setup done, having Require doing things behind the scenes is SO useful.


  • Link dead as of this writing.
    – kontur
    Oct 15, 2017 at 18:46

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.