Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way turn off jQuery noConflict mode in WordPress?

Is there a way turn off jQuery.noConflict in WordPress? I don't mean loading an alternative version of jQuery or changing the loading method ie:

jQuery(document).ready(function( $ ) { ... });

or

(function($) { ... })( jQuery );

I mean is there a way to just turn off noConflict mode for the version of jQuery bundled with WordPress?

Like would setting jQuery.noConflict(false) work? If so, where would you set it?

like image 304
AidanCurran Avatar asked Jul 16 '13 21:07

AidanCurran


People also ask

Why jQuery noConflict () method is being used?

noConflict() method to give control of the $ variable back to whichever library first implemented it. This helps us to make sure that jQuery doesn't conflict with the $ object of other libraries. // Import other Library // Import jQuery Library $. noConflict(); // Code that uses other library's $ can follow here.

What does jQuery noConflict mean?

Definition and Usage The noConflict() method releases jQuery's control of the $ variable. This method can also be used to specify a new custom name for the jQuery variable. Tip: This method is useful when other JavaScript libraries use the $ for their functions.


3 Answers

After some research, this is the best answer I can give you:

$ = jQuery.noConflict(true);

To answer your other question, no you can't pass in false, the attribute is used to control what happens to global variables. You can find the documentation here: http://api.jquery.com/jQuery.noConflict/

Also note you could load 2 different versions of jQuery as it suggests (but is not recommended).

like image 116
Seth C. Avatar answered Oct 12 '22 11:10

Seth C.


If you are including your own javascript library or scripts you can add the following to the very top:

 var $ = jQuery;
like image 21
DMTintner Avatar answered Oct 12 '22 12:10

DMTintner


I found another way of making the variable $ available globally. Just put the following in your theme's functions.php or in a plugin:

function so17687619_jquery_add_inline() {
    wp_add_inline_script( 'jquery-core', '$ = jQuery;' );
}
add_action( 'wp_enqueue_scripts', 'so17687619_jquery_add_inline' );

This will output $ = jQuery; as an inline script immediately after the script tag for jQuery. So any scripts included after have the jQuery instance available as $ and jQuery.

like image 36
JHoffmann Avatar answered Oct 12 '22 12:10

JHoffmann