◦ Comprehensive security
◦ 24/7 support
WordPress Guide → Development → Use jQuery
How to use jQuery in WordPress: a beginner’s guide
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:
- Animate menus and UI components
- Add AJAX functionality to forms and comments
- Create lightboxes and galleries
- Handle frontend interactions without writing raw JavaScript
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:
- jQuery
- jQuery UI
- jQuery Migrate (for legacy compatibility)
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:
- Open your site in a browser.
- Right-click and choose Inspect, then go to the Console.
- Type jQuery.fn.jquery and press enter.
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:
- Scripts are loaded only once
- Dependencies are handled automatically
- Files are placed in the right part of the page (<head> or before </body>)
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:
- Ensures jQuery loads before your custom script (via the dependency array)
- Loads your script from /js/custom.js in your theme
- Loads the script in the footer (true = before </body>)
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:
- Always wait for $(document).ready() before manipulating elements.
- Test your code using browser dev tools to catch syntax or loading issues.
Where to place your jQuery scripts
Where you store your scripts depends on whether you’re building a theme or a plugin.
- Themes: Create a /js/ folder inside your theme and load files using get_template_directory_uri().
- Child themes: Use get_stylesheet_directory_uri() instead to avoid breaking compatibility.
- Plugins: Place your JS files inside the plugin folder and use plugins_url().
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:
- Is jQuery actually loading? Use browser dev tools (Console tab) and type jQuery or jQuery.fn.jquery.
- Is $ defined? If not, you’re probably using it outside of a safe wrapper.
- Are your scripts loading in the correct order? Make sure jQuery is a declared dependency.
- Is wp_footer() or wp_head() missing? Many scripts rely on these functions being present in your theme files.
- Are you seeing console errors? Syntax mistakes or typos can break all JavaScript on the page.
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:
- Break plugins or themes that rely on WordPress’s version
- Lead to version mismatches
- Add unnecessary overhead to your site’s load time
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.
Next steps for using jQuery in WordPress
Using jQuery can simplify your WordPress customizations, but only if you load it the right way and write safe, compatible code. Now that you understand how WordPress handles jQuery, you can use it confidently in your themes and plugins.
Start by enqueuing jQuery the correct way in your theme’s functions.php file, then write your scripts using the jQuery() syntax or a safe wrapper function. Use browser dev tools to debug issues and avoid hardcoding scripts in your theme templates.
Ready to upgrade your WordPress experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy. Liquid Web’s WordPress hosting options configure business-class servers and support plans specifically for WordPress websites.
Don’t want to deal with server management and maintenance? Our fully managed hosting for WordPress is the best in the industry. Our team are not only server IT experts, but WordPress hosting experts as well. Your server couldn’t be in better hands.
Click through below to explore all of our hosting for WordPress options, or chat with a WordPress expert right now to get answers and advice.
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!