Why is jQuery Not Defined in WordPress, even though My Script is Enqueued Properly?

If you are facing a “jQuery is not defined” error in WordPress even though your script is enqueued properly, the most likely cause is the use of jQuery’s noConflict mode in WordPress.

WordPress, by default, runs jQuery in a noConflict mode, a mode that is designed to prevent conflicts with other JavaScript libraries that use the dollar sign ($) as their function or variable name.

In noConflict mode, the $ shortcut is not available and jQuery should be used instead. So, if your script looks something like this:

    // your code here

You’ll need to change it to:

    // now you can use $ within this function

In the above example, jQuery is used to start the function, and $ is passed as an argument, allowing you to use $ safely inside the function.

Another reason could be the loading order of your scripts. Make sure jQuery is loaded before your script. When you enqueue your script, you should specify jQuery as a dependency. Here’s an example:

wp_enqueue_script('my_script', get_template_directory_uri() . '/js/my_script.js', array('jquery'), '1.0', true);

Here, 'jquery' is set as a dependency, which means WordPress will load jQuery before my_script.

Leave a Comment