Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Just a hint: Link to source. –  kaiser Jun 5 '12 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. –  goldenapples Jun 5 '12 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 –  Nicola Peluchetti Jun 5 '12 at 23:31

2 Answers 2

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.

https://github.com/chrisjhoughton/WordPress-plugin-with-RequireJS

share|improve this answer

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.)

share|improve this answer
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. –  Nicola Peluchetti Jun 22 '12 at 14:47
    
Just to know, how do you handle the possibility that another plugin might use requirejs? –  Nicola Peluchetti Jun 29 '12 at 15:57
    
@NicolaPeluchetti As of now, I don't :-( –  KajMagnus Jun 29 '12 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 –  Nicola Peluchetti Jun 29 '12 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 '12 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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