Why can't we use "$" instead if jQuery in wordpress? Usually "$" is the alternative of jQuery right? But in Wordpress it shows "$" is not a function. I dont get why is that so. Can anyone explain?
noConflict() "frees" the "$" from being associated with jQuery. Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict() you can't do that anymore and so you have to replace each "$" with "jQuery"; .
The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector. It is given a shorter identifier as $ just to reduce the time for writing larger syntax.
In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $ . If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $. noConflict() .
WordPress comes bundled with jQuery and some essential jQuery libraries. WordPress theme and plugin developers can easily call jQuery in their own plugins and themes to add their own jQuery scripts. To call jQuery in WordPress theme or plugin, users need to add their own jQuery scripts and enqueue them in WordPress.
Have a look at tip 5 in this web page: 5 Tips for using jquery with wordpress:
It is important to know that the version of jQuery that comes with WordPress automatically calls the jQuery.noConflict(); function, which gives control of the $ variable back to whichever library first implemented it. If you are loading a different copy of jQuery, you'll need to manually call jQuery.noConflict();, if necessary, from one of your JavaScript files.
I hope that helps.
The tricky thing is this particular copy of jQuery is in compatibility mode by default. That means that the typical ‘$’ shortcut for jQuery doesn’t work, so it doesn’t conflict with any other JavaScript libraries that use the dollar sign also, like MooTools or Prototype.
Source: Using ‘$’ instead of ‘jQuery’ in WordPress
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With