16

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?

Brooke.
  • 3,691
  • 13
  • 49
  • 80
AidanCurran
  • 2,460
  • 5
  • 20
  • 24
  • 1
    You'll find more specialists at [wordpress.se]. But, please, check the [etiquette for cross-posting](http://meta.stackexchange.com/q/64068/185667) first. Hint: anyone who knows a solution or workaround will ask you *why you want to do it*. – brasofilo Jul 16 '13 at 23:33

6 Answers6

17

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).

Seth C.
  • 357
  • 2
  • 11
  • where do you set `$ = jQuery.noConflict(true);`? – AidanCurran Jul 16 '13 at 23:43
  • 1
    @AidanCurran http://tommaitland.net/2012/10/disabling-jquery-no-conflict-mode-in-wordpress/ – Jacksonkr Oct 23 '13 at 14:54
  • you would set this code anytime AFTER the jquery noconflict is setup but also it has to be BEFORE any of your $ prefixed code. If all your code is in 1 js file... just put it at the top. – Seth C. Oct 24 '13 at 16:47
  • My particular situation worked with `$ = jQuery.noConflict();`, as in the linked example in @Jackson's comment. – goodeye May 25 '14 at 23:02
6

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

 var $ = jQuery;
DMTintner
  • 14,405
  • 5
  • 35
  • 32
6

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.

JHoffmann
  • 722
  • 8
  • 17
0

Adding this worked for me finally:

var $ = jQuery.noConflict();

You can add this in your header.php file in the head section:

<script>var $ = jQuery.noConflict();</script>

Or if you use child theme, add that to functions.php in your child theme directory:

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/main.js',
        array( 'jquery' )
    );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

And create a main.js file in the same location as functions.php (in the child theme direcotry) and in that file add this:

var $ = jQuery.noConflict();
Mike
  • 1,258
  • 12
  • 12
0

You had better write in functions.php

function wp_deregister_script is for Remove a registered script.

function remove_default_jquery()
{
  // if is not admin screen
  if (!is_admin()) {
//this function
    wp_deregister_script('jquery');
}


//wp_enqueue_scripts is WordPress fook for JavaScript
add_action('wp_enqueue_scripts', 'remove_default_jquery');

When you use jQuery in WordPress

You can use jQuery like as usual.

<script type="text/javascript">

//jQuery in WordPress
jQuery(function($){
    $('.carousel').carousel();
    $('.count_num').counterUp({
    delay: 10,
    time: 1000
    });
});
</script>

Public document is here

https://developer.wordpress.org/reference/functions/wp_deregister_script/

Hideyasu.T
  • 809
  • 6
  • 5
-1

To turn if off go to your wp-includes/js/jquery/jquery.js file and remove the jQuery.noConflict() from the last line. Or as you suggested just set the boolean to false.

That or you could replace the contents with a clean download from jquery.com, there is an intense debate going on in the trac.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
vimes1984
  • 1,684
  • 3
  • 26
  • 59
  • 1
    I wouldn't want to modify that file as it would be overwritten with WP update. Thanks for link to Trac debate, I'm in favor of removing it or, better still, making it a setting. – AidanCurran Jul 16 '13 at 23:46
  • 2
    well yes it would overwrite on update but apart from that as the debate in the trac says there isn't another way to stop it. The option matt is giving you below is to be placed in your js file. but for that you might aswell just use the wrapper. I set a shortcode tab in sublime to add the (function($){}(jquery); in just two clicks :D – vimes1984 Jul 16 '13 at 23:50