WordPress GuideDevelopment → Use jQuery

How to use jQuery in WordPress: a beginner’s guide

Image

Need to add some interactive elements to your WordPress site—like dropdowns, sliders, or form validation? jQuery makes all of that simpler. But to use it in WordPress, you have to do it the WordPress way.

Let’s walk through exactly how to load jQuery, write compatible code, and keep your site running smoothly without breaking themes or plugins.

Get fast, reliable hosting for WordPress

Power your site with the industry’s fastest, most optimized WordPress hosting

What is jQuery and why use it in WordPress?

jQuery is a lightweight JavaScript library that simplifies tasks like DOM manipulation, event handling, animations, and AJAX calls. You’ll find it used in sliders, dropdown menus, form validations, and dynamic content displays.

In WordPress, jQuery is often used to:

That said, jQuery is not always the best choice. For large or complex projects, especially those using modern front-end frameworks or React via the WordPress Block Editor, vanilla JavaScript or custom JS frameworks might be a better fit.

Is jQuery already included in WordPress?

Yes, WordPress includes jQuery out of the box. In fact, it comes with several core JavaScript libraries:

You don’t need to add it manually or pull from a CDN—just enqueue it properly, and it’s ready to go.

To check which version is bundled with your site:

WordPress loads jQuery in noConflict mode, which disables the use of the $ shorthand by default to avoid conflicts with other libraries.

How WordPress handles JavaScript and jQuery

In WordPress, JavaScript files—including jQuery—should always be loaded using the wp_enqueue_script() function. This ensures:

You also need to understand that $ won’t work unless you wrap it safely, since WordPress uses noConflict to prevent clashes with other scripts.

Use jQuery instead of $ unless you’re writing your code in a special wrapper. More on that in a minute.

How to properly add jQuery to your WordPress theme or plugin

The right way to load jQuery is by enqueueing it in your theme’s functions.php file or inside a plugin.

Enqueueing jQuery in a theme

Here’s how to enqueue jQuery and your own custom script in a theme:

function mytheme_enqueue_scripts() {
  // Load WordPress’s built-in jQuery
  wp_enqueue_script(‘jquery’);

  // Load your custom script that depends on jQuery
  wp_enqueue_script(
    ‘custom-script’,
    get_template_directory_uri() . ‘/js/custom.js’,
    array(‘jquery’),
    null,
    true
  );
}
add_action(‘wp_enqueue_scripts’, ‘mytheme_enqueue_scripts’);

This code does a few important things:

Enqueueing jQuery in a plugin

If you’re building a plugin, the process is nearly identical—just use plugins_url() to set the path:

function myplugin_enqueue_scripts() {
  wp_enqueue_script(‘jquery’);
  wp_enqueue_script(
    ‘plugin-script’,
    plugins_url(‘js/plugin-script.js’, __FILE__),
    array(‘jquery’),
    null,
    true
  );
}
add_action(‘wp_enqueue_scripts’, ‘myplugin_enqueue_scripts’);

How to write jQuery code that works in WordPress

Because of WordPress’s noConflict setup, you can’t use the $ shorthand in jQuery like you would elsewhere—unless you wrap your code in a safe scope.

Here’s how to write jQuery the right way in WordPress:

(function($){
  $(document).ready(function(){
    // Your code here
    $(‘.menu-toggle’).click(function(){
      $(‘.main-menu’).slideToggle();
    });
  });
})(jQuery);

This self-invoking function passes in jQuery and uses $ safely within that scope.

Don’t forget:

Where to place your jQuery scripts

Where you store your scripts depends on whether you’re building a theme or a plugin.

Never hardcode <script> tags directly in header.php or footer.php. Use wp_enqueue_script() so that dependencies are handled properly and scripts only load when needed.

Using jQuery in the WordPress admin area

If you need jQuery in the admin dashboard—for example, on a settings page or custom metabox—you should enqueue it separately.

Use the admin_enqueue_scripts hook:

function myplugin_admin_scripts($hook) {
  // Only load on post editor pages
  if ($hook !== ‘post.php’ && $hook !== ‘post-new.php’) {
    return;
  }

  wp_enqueue_script(‘jquery’);
  wp_enqueue_script(
    ‘admin-custom-script’,
    plugins_url(‘js/admin.js’, __FILE__),
    array(‘jquery’),
    null,
    true
  );
}
add_action(‘admin_enqueue_scripts’, ‘myplugin_admin_scripts’);

Always check the $hook to avoid loading scripts on every admin screen.

Debugging jQuery in WordPress

If your jQuery code isn’t working, run through this checklist:

Bonus: how to use jQuery from a CDN (and why you probably shouldn’t)

You can load jQuery from Google or another CDN, but it’s usually not worth it. WordPress already includes jQuery, and using an external version can:

If you still want to go this route, deregister the built-in jQuery and register your own:

function load_cdn_jquery() {
  if (!is_admin()) {
    wp_deregister_script(‘jquery’);
    wp_register_script(‘jquery’, ‘https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js’, false, null, true);
    wp_enqueue_script(‘jquery’);
  }
}
add_action(‘wp_enqueue_scripts’, ‘load_cdn_jquery’);

Use this with caution, and test thoroughly.

jQuery alternatives and the future of JavaScript in WordPress

jQuery is still useful for small tweaks and frontend UI interactions, but modern WordPress is moving toward native JavaScript and frameworks like React.

Gutenberg blocks are built using React. For serious customizations, especially in the admin interface or with block development, learning modern JavaScript is the better long-term path.

Still, jQuery remains a solid option for many frontend effects—especially if you’re maintaining a classic theme or adding quick interactivity without diving into build tools.

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

Additional resources

What is managed WordPress hosting? →

Get details and decide if managed WordPress hosting is right for you.

How to install WordPress with Nginx →

Learn how to install and configure WordPress with NGINX for improved performance and flexibility.












A complete guide to WordPress shortcodes →

Shortcodes make life easier. Learn how to get started!

Trust us to help you choose the ideal hosting solution

Loading form…