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?
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.
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.
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).
If you are including your own javascript library or scripts you can add the following to the very top:
var $ = jQuery;
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
.
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