The “Uncaught TypeError: $ is not a function” error in WordPress typically occurs when jQuery is not loaded correctly or when there is a conflict between jQuery and other JavaScript libraries. Here’s how you can fix this error:
1. Ensure jQuery is Enqueued Correctly
Make sure that jQuery is enqueued properly in your WordPress theme or plugin. You can do this by adding the following code to your theme’s functions.php
file:
function enqueue_jquery() {
if (!is_admin()) {
wp_enqueue_script('jquery');
}
}
add_action('wp_enqueue_scripts', 'enqueue_jquery');
This ensures that jQuery is loaded on the front end of your site.
2. Use jQuery NoConflict Mode
WordPress uses jQuery
in noConflict mode by default to avoid conflicts with other JavaScript libraries. This means you cannot use the $
shortcut directly. Instead, you should use jQuery
or wrap your code to use $
within a jQuery noConflict wrapper.
Here’s an example of wrapping your code:
jQuery(document).ready(function($) {
// Your jQuery code using $
$('selector').action();
});
3. Check for Multiple jQuery Inclusions
Ensure that jQuery is not being included multiple times. Sometimes themes or plugins might enqueue their own versions of jQuery, leading to conflicts. Check your theme and plugin files to ensure jQuery is enqueued correctly and only once.
4. Debugging with the Browser Console
Use your browser’s developer console to check for any JavaScript errors or multiple inclusions of jQuery. Here’s how you can open the console:
- Chrome: Right-click on the page, select “Inspect,” then go to the “Console” tab.
- Firefox: Right-click on the page, select “Inspect Element,” then go to the “Console” tab.
- Edge: Right-click on the page, select “Inspect Element,” then go to the “Console” tab.
Look for errors related to jQuery and see if there are multiple versions loaded.
5. Deactivate Plugins
If you suspect a plugin conflict, try deactivating your plugins one by one to identify which one is causing the issue. After deactivating a plugin, check if the error persists. Once you find the conflicting plugin, you can look for alternatives or contact the plugin author for a fix.
6. Update WordPress, Theme, and Plugins
Ensure that your WordPress installation, theme, and plugins are up to date. Sometimes, updating to the latest version can resolve compatibility issues.
7. Custom jQuery Initialization
If you need to use $
without the jQuery
wrapper, you can create your own noConflict instance:
var $ = jQuery.noConflict();
$(document).ready(function() {
// Your jQuery code using $
$('selector').action();
});
Example Code Correction
If your code looks like this and throws the error:
$(document).ready(function() {
$('selector').action();
});
Change it to:
jQuery(document).ready(function($) {
$('selector').action();
});
By following these steps, you should be able to resolve the “Uncaught TypeError: $ is not a function” error in WordPress. If the problem persists, consider reaching out to the WordPress community forums or a developer for more specific assistance.