← Tous les articles

WordPress: “jQuery is not defined” or “$ is not a function”

A jQuery script crashes in the console? It is almost always a load-order problem or a missing dependency declaration.

WordPress: “jQuery is not defined” or “$ is not a function”

Key points

  • The script runs before jQuery is loaded.
  • Declare the dependency in wp_enqueue_script.
  • In WordPress, $ is not defined by default: use jQuery or a wrapper.

The console shows “Uncaught ReferenceError: jQuery is not defined” and a slider or form stops working. It often happens right after enabling an optimisation plugin that defers or combines scripts.

Declare the dependency

A script hard-coded in the theme with a <script> tag does not know it needs jQuery. Use wp_enqueue_script and list the dependency; WordPress will load jQuery first.

add_action('wp_enqueue_scripts', function () {
    wp_enqueue_script('my-slider', get_theme_file_uri('js/slider.js'), ['jquery'], '1.2', true);
});

“$ is not a function”

WordPress loads jQuery in noConflict mode, so $ is undefined. Wrap your code:

jQuery(function ($) {
  $('.slider').slick();
});

Optimisation plugins

“Defer JavaScript” or “delay execution” options move jQuery to the end of the page, while an inline script in the HTML needs it immediately. Exclude jquery-core from that option or, better, move the inline script into a file enqueued with the dependency.

Do you still need jQuery?

Our recent themes no longer use jQuery: selectors, events and fetch() are native. Dropping it saves around 30 KB compressed and one render-blocking script. On an existing site, plugins often rely on it: keep it, and write new code in plain JavaScript.

FAQ

Can I load jQuery from a CDN?

Not recommended: the version must match what WordPress expects, and modern browsers no longer share caches across sites.

It only fails when logged out. Why?

Caching and optimisation plugins often apply to visitors only. Test in a private window.

Facing this on your site or app? We diagnose and fix it.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →