0

I've been working on localhost and everything looks fine but then when I uploaded my Wordpress theme online, all of the jQuery plugin functions are undefined!

One solution that worked (which I haven't tested a lot so I still don't know if it will work in the long run) is to import the javascript file from the plugin's main website or github page. I don't really prefer this "solution."

Another is the solution here but I haven't tested this a lot also. My issue here is I've used jQuery in another theme and everything works using just $, but in my new theme it doesn't work with $ and so I have to use $j instead. It's just that I'm very used to using just $.

Can someone explain this weird phenomenon? Thank you! :)

4
  • 2
    Do test the solution you linked to yourself?.. :) Using just $ for jQuery is not supported in WordPress.
    – Rarst
    Commented Oct 11, 2012 at 17:57
  • Is jQuery itself loading when your theme is "live"? Commented Oct 11, 2012 at 18:05
  • What's the site URL? (So we can see the errors). How are you calling the main jQuery library: via wp_enqueue_script in functions.php or by a simple link in header.php? Commented Oct 11, 2012 at 18:13
  • Also, do look up how JS closures work. Always use closures. That fixes your problems and many other JS oddities in one simple step.
    – Miha Rekar
    Commented Oct 11, 2012 at 19:10

2 Answers 2

2

You can use $ but it is advised you don't. Use jQuery or something like the example in your link, if you must use $ you can do so like this;

$.noConflict();
jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
});
   //$ will cause problems if put here:)

To use it for just a function you can append jQuery to the end like:

(function($) {
    // $() will work as an alias for jQuery() 
})(jQuery);  //dont forget this part
2
  • How about functions outside, do I pass $ as an argument as well?
    – ton
    Commented Oct 12, 2012 at 14:19
  • To use $ in this case it must be inside a functions defined as function($) , anything outside that will cause a conflict. But you can use it for any functions if you also append jQuery to the end, Ill edit my answer.
    – Wyck
    Commented Oct 12, 2012 at 16:16
1

This is happening because WordPress loads jQuery in "no conflict" mode.

I'm also in the habit of using $, and there's a fairly simple solution. Just modify your (document).ready function like so:

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
});

For more info, check out the Codex section on this topic.

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