259

I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper like this:

$(document).ready(function(){

    // jQuery code is in here

});

I am calling this script from within the WordPress Dashboard and am loading it AFTER the jQuery framework has loaded.

When I check the page in Firebug I constantly keep receiving the error message:

TypeError: $ is not a function

$(document).ready(function(){

Should I maybe wrap the script in this function:

(function($){

    // jQuery code is in here

})(jQuery);

I have had this error quite a few times and am not sure how to handle it.

Any help would be greatly appreciated.

2
  • 3
    If you type $ in console and press enter - what do you see?
    – zerkms
    Sep 9, 2012 at 23:08
  • When running $ in console I receive "undefined"
    – Jason
    Sep 9, 2012 at 23:21

19 Answers 19

365

By default when you enqueue jQuery in Wordpress you must use jQuery, and $ is not used (this is for compatibility with other libraries).

Your solution of wrapping it in function will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).

If you must use document.ready, you can actually pass $ into the function call:

jQuery(function ($) { ...
4
  • I tried the second version however it didn't work, the first version did but I just want to make sure I am not doubling up on calling jQuery.
    – Jason
    Sep 9, 2012 at 23:19
  • @Jason you don't even need that; just exactly what I posted: jsfiddle.net/vcbYJ Sep 9, 2012 at 23:24
  • Could you please see this question wordpress.stackexchange.com/questions/214858/…
    – user5742826
    Jan 18, 2016 at 13:21
  • And if any selector is outside jQuery(function ($) { ..., you have to use jQuery instead of $ Oct 13, 2017 at 10:13
176

This should fix it:

jQuery(document).ready(function($){
  //you can now use $ as your jQuery object.
  var body = $( 'body' );
});

Put simply, WordPress runs their own scripting before you can and they release the $ var so it won't collide with other libraries. This makes total sense, as WordPress is used for all kinds of web sites, apps, and of course, blogs.

From their documentation:

The jQuery library included with WordPress is set to the noConflict() mode (see wp-includes/js/jquery/jquery.js). This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.

In the noConflict() mode, the global $ shortcut for jQuery is not available...

0
34

This solution worked for me

;(function($){
    // your code
})(jQuery);

Move your code inside the closure and use $ instead of jQuery

I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma

...after searching too much

2
  • 2
    Fantastic solution. The main reason this works is because that ";" closes any other instance of jquery already active and at the same time it declares another one new locally. When you have just few seconds to implement a layout in production or lots and lots of wordpress plugins running this can do it great. But, be aware that you must fix the issue, this is just a band-aid, but if it is an info page it's OK.
    – Luis
    Dec 12, 2015 at 0:53
  • @Martha James This works for me so included it. It may or maynot work for others Apr 29, 2016 at 5:52
33
var $=jQuery.noConflict();

$(document).ready(function(){
    // jQuery code is in here
});

Credit to Ashwani Panwar and Cyssoo answer: https://stackoverflow.com/a/29341144/3010027

Reasons why in WordPress jQuery and not $ is used: https://pippinsplugins.com/why-loading-your-own-jquery-is-irresponsible/

0
22

You can avoid confliction like this

var jq=jQuery.noConflict();
jq(document).ready(function(){  
  alert("Hi this will not conflict now");
  jq('selector').show();
});
1
  • 9
    Or $=jQuery.noConflict();
    – Cyssoo
    Dec 29, 2015 at 14:24
14

Try this:

<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script>
<script>
    jQuery.noConflict();
    (function ($) {
        function readyFn() {
            // Set your code here!!
        }

        $(document).ready(readyFn); 
    })(jQuery);

</script>
0
11

Also, I find the good solution for use jQuery noConflict mode.

(function($){

  $(document).ready(function(){
      // write code here
  });

  // or also you can write jquery code like this

  jQuery(document).ready(function(){
      // write code here
  });

})(jQuery);

I found this solution from here TryVary.com.

10

replace $ sign with jQuery like this:

jQuery(function(){
//your code here
});
9

You have to pass $ in function()

<script>
jQuery(document).ready(function($){

// jQuery code is in here

});
</script>
1
  • 4
    How is this any different than the answer given above (from three years prior)?
    – rnevius
    Feb 3, 2016 at 4:27
9

Use

jQuery(document).

instead of

$(document).

or

Within the function, $ points to jQuery as you would expect

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

Double check your jQuery references. It is possible that you are either referencing it more than once or you are calling your function too early (before jQuery is defined). You can try as mentioned in my comments and put any jQuery reference at the top of your file (in the head) and see if that helps.

If you use the encapsulation of jQuery it shouldn't help in this case. Please try it because I think it is prettier and more obvious, but if jQuery is not defined you will get the same errors.

In the end... jQuery is not currently defined.

2
  • I think it's rather than something tries to access JQuery before it's defined which in turn breaks everything. Try moving your script which references JQuery to the top of your index.html and it will probably work May 18, 2015 at 20:36
  • This answer needs more visibility. Yes, the others are valid if the standard namespace is taken with a version of jQuery loaded via some other plugin. But do check you're not inadvertently (and possibly unnecessarily) loading jQuery more than once.
    – edkay
    Jan 13, 2017 at 19:57
7
(function( $ ) {
  "use strict";

  $(function() {

    //Your code here

  });

}(jQuery));
5

What worked for me. The first library to import is the query library and right then call the jQuery.noConflict() method.

<head>
 <script type="text/javascript" src="jquery.min.js"/>
 <script>
  var jq = jQuery.noConflict();
  jq(document).ready(function(){
  //.... your code here
    });
 </script>
4
<script>
var jq=jQuery.noConflict();
(function ($) 
    {
    function nameoffunction()
    {
        // Set your code here!!
    }

    $(document).ready(readyFn); 
    })(jQuery);

now use jq in place of jQuery

4

You come across this issue when your function name and one of the id names in the file are same. just make sure all your id names in the file are unique.

3

You can use

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

or

(function ($) { ...... }(jQuery));
0
wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

If you are using jquery for frontend, you can achieve it by passing $deps as jquery

0

Had the Uncaught TypeError: $ is not a function error. Though this is not specific to Wordpress, this tip should be useful if you are working on a CMS. Basically, this error tells us that jQuery wasn't defined, particularly $. Chances are you're CMS already declared jQuery. To check, simply go to the web console and see what is attached to the window object.

The jQuery library exposes its methods and properties via two properties of the window object called jQuery and $. $ is simply an alias for jQuery and it's often employed because it's shorter and faster to write.

In my case, the CMS exposed it as cmJQuery (CoreMedia CMS). So the correct code looks something like:

cmJQuery(document).ready(function($) {
  //you can now use $ as your jQuery object.
});

It was a good thing it had a descriptive name!

cmJQuery

As you can see below, it had the same function names.

jQuery

0

I know this is an old question but I came upon it with a similar issue after some optimizations I did in newer versions of WordPress (6+) and this did the trick for me:

/**
 * Ensure jQuery $ Always Exists
 *
 * Define the global `$` variable after jQuery is loaded if it doesn't already exist.
 *
 * @return void
 */
function au_ensure_jquery()
{
    wp_add_inline_script('jquery-core', 'window["$"]=$||jQuery.noConflict();');
}
add_action('wp_enqueue_scripts', 'au_ensure_jquery', 0, 0);

With this PHP code, it will automatically define the global $ variable if it's not already defined immediately after jQuery is loaded on the page. Of course, this means it can't be deferred or loaded asynchronously. It also only adds this snippet if jQuery is enqueued on the page because it's connected to the jquery-core handle.

Additionally, I use the snippet window["$"] = $ || jQuery.noConflict(); to replace var $ = jQuery.noConflict(); in any of my scripts to ensure it either gets the existing global $ variable or defines it.

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