jQuery Is Not Defined in WordPress, But My Script Is Enqueued Properly
When you see "jQuery is not defined" in the browser console while debugging a custom script enqueued via wp_enqueue_script, the immediate suspicion often falls on the enqueue logic. You verify that the handle exists, the path is correct, and the hook wp_enqueue_scripts is firing. Yet, the error persists. The culprit is rarely your PHP code; it is almost always a dependency declaration issue or a scope problem within the JavaScript itself.
In the specific case provided, the script is being loaded correctly:
function lapetitefrog_scripts() {
wp_enqueue_style( 'lapetitefrog-style', get_stylesheet_uri() );
wp_enqueue_script(
'lapetitefrog-mobile-menu',
get_template_directory_uri() . '/js/mobile-menu.js',
array(), // Dependency array is empty here
'1.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'lapetitefrog_scripts' );
The function registers a script named lapetitefrog-mobile-menu with the handle 'lapetitefrog-mobile-menu'. Crucially, the third argumentâthe dependency arrayâis empty. This tells WordPress that this script does not rely on any other assets. However, the JavaScript file expects jQuery to be present. If jQuery is not explicitly declared as a dependency in the wp_enqueue_script call, WordPress will load the custom script immediately after parsing the page head or body, potentially before the global $ alias for jQuery has been initialized by the core library.
The Dependency Mismatch
WordPress enqueues scripts sequentially based on the order they are called within an action hook, but it also respects dependency arrays to ensure specific libraries load first. When you define a script like this:
wp_enqueue_script( 'lapetitefrog-mobile-menu', $path, array(), '1.0', true );
You are telling WordPress: "Load this file regardless of what else is on the page." If your theme loads jQuery via wp_enqueue_script with a handle like 'jquery', and that call happens in a separate function or earlier in the action queue without being linked to the current script, you still need to tell WordPress about the relationship.
The fix is straightforward but often overlooked: add the handle of the jQuery library to the dependency array. In modern WordPress themes (typically Twenty Twenty-Four and later) or those using wp_enqueue_script for jQuery in the theme header, the handle is usually 'jquery'. For older themes that might be loading a different version or from a plugin, you must identify the exact handle used by your site's jQuery instance.
Update your enqueue function to look like this:
function lapetitefrog_scripts() {
wp_enqueue_style( 'lapetitefrog-style', get_stylesheet_uri() );
// Add 'jquery' as a dependency so WordPress ensures it loads first
wp_enqueue_script(
'lapetitefrog-mobile-menu',
get_template_directory_uri() . '/js/mobile-menu.js',
array( 'jquery' ), // Dependency array populated
'1.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'lapetitefrog_scripts' );
Inspecting the JavaScript Scope
Once the PHP side is corrected to declare the dependency, you must verify that the JavaScript file is using the global object correctly. The provided mobile-menu.js uses $:
jQuery(document).ready(function($) {
$('#menu-icon').click(function() {
$('#switchmenu').slideToggle("fast");
});
});
This code invokes jQuery immediately, then passes a callback to the document ready method where it assigns this (the global jQuery object) to the variable $. This is a valid pattern known as "Immediately Invoked Function Expression" (IIFE) with argument passing. However, if your project uses ES modules (type="module" in the <script> tag), the global $ will not exist unless you explicitly import it or use window.jQuery.
If the script remains an asynchronous module, change the IIFE to accept the global object as the first argument:
(function($) {
// Ensure we are checking for the global window object if needed
if (typeof window === 'undefined' || typeof window.$ === 'undefined') {
return;
}
$('#menu-icon').click(function() {
$('#switchmenu').slideToggle("fast");
});
})(jQuery);
Why This Matters for Modern Development
While WordPress core still ships with jQuery, the ecosystem is shifting. Many developers now prefer Vanilla JS or libraries like Alpine.js to avoid the overhead of a massive global library. If you are building a theme for a LaravelCompany project or any modern stack, consider whether the custom menu toggle requires jQuery at all. A simple event delegation using plain JavaScript can often replace .click() and .slideToggle() without needing a dependency array in your wp_enqueue_script call.
However, if legacy code or third-party plugins necessitate jQuery, ensuring the dependency is declared in functions.php prevents the "jQuery is not defined" error by respecting WordPress's asset loading lifecycle. Always verify that the handle of the external library matches the handle expected by your custom script.