Introduction
Facing the ‘jQuery is not defined’ error in WordPress? Learn 6 easy fixes, including script enqueuing, resolving plugin conflicts, and updating your site for smooth functionality.
WordPress is one of the most popular website-building platforms globally. Its flexibility and ease of use attract millions of users, from beginners to professionals. However, like any platform, it isn’t immune to errors. One common issue users face is the ‘jQuery is not defined’ error. This error can disrupt your website’s functionality, causing features like sliders, menus, and forms to stop working.
It will also discuss 6 effective ways of fixing the error. Let’s dive in!
What Is the ‘jQuery is Not Defined’ Error?
Before jumping into the solutions, let’s clarify what this error actually means.
jQuery is a JavaScript library that allows for easier coding, and it simplifies common tasks such as manipulating HTML, handling events, and creating animations. By default, WordPress uses jQuery for many of its built-in features. The ‘jQuery is not defined’ error pops up when your website fails to load jQuery correctly. What this error really says is that when trying to run JavaScript, the browser can’t locate the jQuery library.
Why Does This Error Happen?
The ‘jQuery is not defined’ error in WordPress can be triggered for several reasons, which are as follows:
- Improper Loading Order: Loading jQuery after all the other scripts that call jQuery may signal this error.
- Conflicting Plugins or Themes: There are plugins or themes that do not enqueue jQuery properly.
- CDN Issues: Using a CDN to load jQuery and network issues or incorrect URL usage can result in an error.
- Missing wp_enqueue_script: This is a case where developer fails to enqueue scripts correctly, which causes WordPress to malfunction. Skipping proper enqueue may lead to problems.
- Deferred or Asynchronous Loading: If jQuery was loaded on defer or asynchronously, dependent scripts may try to execute before it’s fully loaded.
Now that we understand the problem, let’s take a look at 6 ways to fix this.
1. Checking for Proper Script Enqueuing
The reason is that WordPress requires all scripts to be enqueued through the function wp_enqueue_script for loading. Poorly loaded scripts cause this error in a very large number of cases.
Steps to Fix:
- Access Your Theme Files: Go into your WordPress Dashboard > Appearance > Theme Editor.
Locate the functions.php file.
- Ensure Correct Enqueueing: Add the following code snippet to enqueue jQuery correctly:
{
php
Copy code
function load_custom_scripts() {
wp_enqueue_script(‘jquery’);
}
add_action(‘wp_enqueue_scripts’, ‘load_custom_scripts’);
This ensures that WordPress’s default version of jQuery is loaded.
- Save Changes: After adding the code, save your functions.php file and check your website.
2.Resolve Plugin or Theme Conflicts
Plugins or themes can override WordPress’s default jQuery library, leading to conflicts.
Steps to Fix:
- Deactivate All Plugins: Go to Plugins > Installed Plugins in your WordPress dashboard. Deactivate all plugins temporarily.
- Check Your Website: Visit your site to see if the error persists. If the error is gone, then the problem has to do with a plugin conflict.
- Enable Plugins One by One: Enable each plugin one at a time and check your website after every activation. Locate the problematic plugin and either contact its developer or switch to an alternative.
- Switch Themes: Temporarily change to a default WordPress theme like Twenty Twenty-One. If the error clears up, then your theme may be at fault.
3. Check CDN Settings
This could be due to an incorrect setting if one uses a Content Delivery Network to serve jQuery.
Fixing Steps:
- Check the CDN URL: Verify that the URL of your jQuery library in your CDN is correct and the most updated.
- Switch to the jQuery Local Version: In the functions.php file, edit to load the jQuery locally instead of using a CDN:
{
php
Copy code
function load_local_jquery() {
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, includes_url(‘/js/jquery/jquery.js’), false, null, true);
wp_enqueue_script(‘jquery’);
}
add_action(‘wp_enqueue_scripts’, ‘load_local_jquery’);
- Clear CDN Cache: Clear your CDN cache to make sure changes are in effect.
4. Disable Deferred or Asynchronous Loading
Most optimization plugins defer or load JavaScript asynchronously to improve page speed. But this often makes jQuery-dependent scripts fail.
Steps to Fix:
- Look into Optimization Plugins: Go to the settings of optimization plugins such as WP Rocket, Autoptimize, or W3 Total Cache. Check the option Defer JavaScript or Load JavaScript Asynchronously.
- Exclude jQuery: Add jquery.js to the exclusion list for deferred/asynchronous loading.
- Save Changes and Test: Save the changes of the plugin settings and test your website to make sure the error is resolved.
5. Update WordPress, Themes, and Plugins
An outdated WordPress version or theme or plugins may result in incompatibility with jQuery.
Steps to Fix:
- Backup Your Website: Take a backup of your site using any plugin such as Updraft Plus prior to updates.
- Update WordPress: Head to Dashboard > Updates and update WordPress to the latest version available. Update Themes and Plugins: Update all your installed themes and plugins to their latest versions.
- Check Compatibility: Make sure all your plugins and themes are compatible with the latest version of WordPress.
6. Implement the jQuery Migrate Helper Plugin
The jQuery Migrate tool was excluded by default in WordPress 5.5 and later. This creates incompatibility with older plugins and themes using jQuery functions that are deprecated.
Steps to Fix:
- Install jQuery Migrate Helper Plugin: Find Plugins >> Add New and search for Enable jQuery Migrate Helper. Install and activate the plugin.
- Test Your Website: Access your site to find out whether this bug is fixed. The plugin temporarily reinstates the jQuery Migrate script, which will give you the ability to sense compatibility issues.
- Update Code: From the plugin’s logs, identify legacy code in your theme or plugins. Contact developers for code updates or find modern alternatives right away.
- Disable the Plugin: Once the bug is fixed, disable the plugin to avoid loading any more extra scripts on your site.
Conclusion
The ‘jquery not defined’ error in WordPress is frustrating, but fixable if approached properly. Below are six ways: checking script enqueuing, resolving conflicts, verifying CDN settings, disabling deferred loading, updating software, and using the jQuery Migrate Helper plugin-that will help resolve the issue and give your website back the needed functionality.
If you’re unsure about making these changes, consider seeking help from a WordPress expert Contact us . Keeping your site error-free ensures a better user experience and smoother operations.