Re: WordPress and jQuery - loading but not working

In short, to allow multiple libraries like jQuery and Prototype to be loaded side by side into the same page - both of which use the global $ shortcut by default - WP uses jQuery's .noConflict() function by default, so the $ global no longer refers to the jQuery object itself. jQuery then needs to be used by calling the actual jQuery object instead.

Any code within the outer function will then use $ to reference jQuery, like you normally get with jQuery code.

P.S.
Just because WP allows multiple libraries to be loaded at the same time, doing so is still a bad idea. It's more or less a waste of resources to load multiple libraries for different purposes when one of them would work just fine for both.

It's unfortunately way to common to see WP sites that loads multiple versions of jQuery and/or other libraries into the page, because the designer just copied and pasted snippets from various online examples to get a few different JS effects running, without considering that each of them is referencing a library on it's own. - It's even more unfortunate that WP seems to encourage this behavior.

Re: WordPress and jQuery - loading but not working

Posted 29 March 2013 - 04:50 AM

Thanks Ali,

A brilliant reply. It works perfectly now.

The next thing is to test to see if jQuery is already running on the site and if so just run the plugin script.

Even better would be test to see if the script is even needed on the page. I prefer to only load what is absolutely necessary - much better for those out and about on a 3G network. But I'm just a hobby developer and am over the moon if something actually works. Getting it to be efficient is a whole new level of skill that I don't really have.