Fix TypeError: $ is not a function When Using jQuery in WordPress

Quick Summary
- WordPress calls jQuery.noConflict() when it loads jQuery, so the global $ is undefined and only the name jQuery works.
- Wrap DOM code in jQuery(function ($) {...}), or a whole file in (function ($) {...})(jQuery), to use $ safely inside your own code.
- Enqueue your script with jquery as a dependency; a ReferenceError such as jQuery is not defined means jQuery hadn't loaded when your code ran.
TypeError: $ is not a function on a WordPress site means your script calls $(), but $ isn’t jQuery there. WordPress loads jQuery in no-conflict mode, which gives up the $ name and keeps only jQuery. Either write jQuery in full, or wrap your code in a function that receives jQuery as $.
Why $ isn’t jQuery in WordPress
jQuery normally creates two global names for itself: jQuery and the shortcut $. Other libraries have used $ too, so jQuery has a jQuery.noConflict() method that hands $ back to whatever held it before jQuery loaded.
WordPress calls that method for you: its copy of jQuery in wp-includes/js/jquery/jquery.js ends with jQuery.noConflict();. On most sites nothing held $ before, so after WordPress loads jQuery, $ is undefined and any call to it fails:
$(function () {
// Never runs. The browser reports:
// TypeError: $ is not a function
});It’s a TypeError rather than a ReferenceError because the global $ exists, it just holds undefined. If you see ReferenceError: $ is not defined or jQuery is not defined instead, jQuery hasn’t loaded at all when your code runs. That case is covered further down.
Don’t fix this with window.$ = jQuery;. It undoes no-conflict mode for every script on the page and breaks any script that expects $ to be another library. The wrappers below give you $ only inside your own code.
Fix 1: the ready wrapper
Code that works with elements on the page should run once the DOM is ready. Pass a function to jQuery() and name its first parameter $:
jQuery(function ($) {
$('.menu-toggle').on('click', function () {
$('.site-nav').toggleClass('is-open');
});
});jQuery calls the ready handler with the jQuery object as its first argument, so inside the function $ is jQuery. Outside it, $ is still undefined. The jQuery documentation for ready calls this aliasing the jQuery object.
jQuery(function ($) { ... }) is the short form of jQuery(document).ready(function ($) { ... }). Both work, but since jQuery 3.0 the documentation recommends the short form and lists the .ready() forms as deprecated.
Fix 2: an IIFE that runs right away
Some code doesn’t need the DOM, for example a small jQuery plugin or a helper function. Wrap it in an immediately invoked function expression (IIFE) and pass jQuery in as the argument:
(function ($) {
// Runs as soon as the file loads.
$.fn.highlight = function () {
return this.css('background-color', 'yellow');
};
// Runs when the DOM is ready.
$(function () {
$('.notice').highlight();
});
})(jQuery);The outer function runs immediately, with jQuery as its $ parameter. Because $ is jQuery everywhere inside it, $(function () { ... }) works there too, which makes this a good wrapper for a whole script file: helpers at the top, DOM code in the ready handler.
Load your script with jquery as a dependency
Both wrappers assume jQuery is already on the page when your file runs. Let WordPress handle the order by enqueuing your script with jquery in its dependencies, from your theme’s functions.php:
function mytheme_enqueue_scripts() {
wp_enqueue_script(
'mytheme-main',
get_stylesheet_directory_uri() . '/js/main.js',
array( 'jquery' ),
'1.0.0',
array( 'in_footer' => true )
);
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_scripts' );array( 'jquery' )makes WordPress print its bundled jQuery beforemain.js. Thejqueryhandle loads jQuery together with jQuery Migrate.- The last argument prints the script in the footer. The array form of that argument works since WordPress 6.3; on older versions pass
trueinstead. get_stylesheet_directory_uri()points to the active theme, which is the child theme when one is active.- Don’t add a second copy of jQuery from a CDN. Plugins expect the copy WordPress bundles, and loading a library twice causes errors of its own, such as this._addClass is not a function with jQuery UI.
Similar errors and what they mean
jQuery is not definedor$ is not defined: jQuery isn’t loaded when your code runs, or isn’t loaded at all, for example because the script is hardcoded in a template or enqueued without thejquerydependency. Enqueue it as shown above; a redirect script that depends on jQuery uses the same setup.- Inline scripts break after deferring JavaScript: if you defer JavaScript in WordPress, keep jQuery itself out of it. Otherwise inline code that uses jQuery runs before jQuery loads.
$(...).somePlugin is not a function: jQuery works, but the plugin isn’t loaded yet, or a second copy of jQuery loaded after it and replaced the jQuery object the plugin was attached to. Add the plugin’s handle to your script’s dependencies and remove the extra copy of jQuery.
