Guide · Updated September 2026

Fixing WordPress JavaScript Not Running in Your Theme

If your WordPress theme's JavaScript isn't running as expected, the issue most commonly stems from incorrect enqueuing, script dependencies, or conflicts with other plugins or themes. This guide provides a systematic approach to identify and resolve these problems, ensuring your interactive elements work flawlessly.

Do it yourself in about a minute

Install Themify and get your first conversion free — no credit card.

Chrome browser logoAdd to Chrome — free

Understanding How WordPress Handles JavaScript

WordPress employs a specific system for loading scripts and styles, primarily through its enqueueing functions. This standardized approach helps prevent conflicts, ensures scripts load in the correct order, and maintains website performance. Directly linking JavaScript files in your theme's header or footer HTML is strongly discouraged and is a common cause of issues like JavaScript not running.

When WordPress loads a page, it processes theme and plugin files, looking for calls to `wp_enqueue_script()`. These functions register scripts with WordPress, specifying their source URL, dependencies, version number, and whether they should be loaded in the header or footer. This controlled environment is crucial for maintaining stability, especially when multiple plugins and themes are active. Ignoring this system can lead to jQuery loading multiple times, scripts attempting to run before their dependencies are ready, or even complete failure of interactive elements.

The Correct Way to Enqueue JavaScript in WordPress

The cornerstone of successful JavaScript integration in WordPress is the `wp_enqueue_script()` function, typically called within your theme's `functions.php` file. This ensures your scripts are loaded properly and can interact with WordPress's built-in libraries like jQuery without conflict.

Here's a standard example of how to enqueue a custom JavaScript file named `my-script.js` located in your theme's `js` folder:

  1. Open your theme's `functions.php` file (Appearance → Theme File Editor → functions.php).
  2. Add the following code block, preferably at the end or within a dedicated script-loading section:
  3. ```php function mytheme_enqueue_scripts() { wp_enqueue_script( 'my-custom-script', get_template_directory_uri() . '/js/my-script.js', array('jquery'), '1.0.0', true // Load in footer ); } add_action('wp_enqueue_scripts', 'mytheme_enqueue_scripts'); ```
  4. This code snippet does the following: - `my-custom-script`: This is a unique handle for your script. - `get_template_directory_uri() . '/js/my-script.js'`: Specifies the full URL path to your script. - `array('jquery')`: Declares jQuery as a dependency. This means `my-script.js` will only load after jQuery has loaded. - `'1.0.0'`: The version number of your script. This helps with browser caching. - `true`: Loads the script in the footer (`wp_footer()` action), which is generally recommended for performance, as it allows the HTML and CSS to render first.
  • Create a `js` folder in your theme's root directory.
  • Place your `my-script.js` file inside this `js` folder.
  • Add the following code to your theme's `functions.php` file (preferably within a child theme to avoid losing changes upon theme updates):

Common Reasons WordPress JavaScript Isn't Running in Theme

While correct enqueuing is foundational, several other factors can cause your WordPress JavaScript not to run. Identifying the exact culprit requires a systematic approach.

These are the most frequent reasons developers encounter issues:

  • **Incorrect File Path**: A typo in `get_template_directory_uri()` or the file path will prevent WordPress from finding your script. Double-check that the path matches the actual location of your JavaScript file.
  • **Missing Dependencies**: If your script relies on another library (like jQuery) but doesn't declare it as a dependency using `array('dependency-handle')`, it might attempt to run before the required library is available, leading to errors.
  • **jQuery Conflicts (No-Conflict Mode)**: WordPress loads jQuery in "no-conflict" mode to prevent clashes with other JavaScript libraries. This means you cannot use the `$` shorthand directly for jQuery. You must use `jQuery` instead or wrap your code in a closure that passes `$` as an argument. For example: `(function($) { // your code here })(jQuery);`.
  • **Script Loaded in Header with DOM Dependent Code**: If your script tries to manipulate DOM elements before they exist (because the script loaded in the header and the HTML hasn't rendered yet), it will fail. Loading scripts in the footer (`true` as the last parameter in `wp_enqueue_script()`) mitigates this.
  • **Syntax Errors in Your JavaScript**: Even a small typo or unclosed bracket in your `my-script.js` file can cause the entire script to stop executing. Browser developer consoles are invaluable for catching these.
  • **Plugin Conflicts**: Another plugin might be loading an older version of jQuery, a conflicting library, or poorly written JavaScript that breaks global execution. Temporarily deactivating plugins can help isolate this.
  • **Theme Conflicts**: Similarly, your parent theme or another active theme (if you're modifying a child theme) might have conflicting scripts or enqueueing practices. If you generated your theme using a tool like Themify, you can review its `functions.php` for any custom script enqueues that might interfere.
  • **Missing `wp_head()` or `wp_footer()`**: Your theme's `header.php` and `footer.php` files must contain `<?php wp_head(); ?>` and `<?php wp_footer(); ?>` respectively. These functions are critical hooks where WordPress, themes, and plugins load their scripts and styles.

Troubleshooting Steps: How to Diagnose JavaScript Issues

Effective troubleshooting involves a methodical process to pinpoint where your JavaScript is failing. Don't guess; use the tools at your disposal.

Follow these steps to diagnose why your WordPress JavaScript isn't running:

  1. **Check Your Browser's Developer Console**: This is your first and most crucial step. Right-click on your webpage, select "Inspect" (or "Inspect Element"), and navigate to the "Console" tab. Look for any error messages in red. These will often directly tell you if a script failed to load, if there's a syntax error, or if a variable is undefined (e.g., "`$` is not defined" indicating a jQuery no-conflict issue).
  2. **Verify Script Enqueueing**: a. View your page's source code (right-click -> "View Page Source" or "Show Page Source"). b. Search for your script's filename (e.g., `my-script.js`). c. Confirm it's present and the `src` attribute points to the correct URL. If it's missing, your `wp_enqueue_script()` call is likely incorrect or not being executed.
  3. **Test Dependencies**: If your script depends on jQuery, try running a simple `console.log(jQuery);` in your browser's console. If it returns `undefined`, jQuery isn't loading correctly or at all. Ensure `array('jquery')` is specified in your `wp_enqueue_script()` call.
  4. **Isolate the Issue**: a. Temporarily deactivate all plugins. If your JavaScript starts working, reactivate them one by one to find the conflicting plugin. b. Switch to a default WordPress theme (like Twenty Twenty-Four). If the JavaScript works there, the issue lies within your custom theme.
  5. **Simplify Your Script**: Temporarily reduce your `my-script.js` to a single line, like `console.log('My script is running!');`. If this simple line doesn't log to the console, the issue is with the script's loading or execution environment, not its content. If it does log, then the problem is within your actual script code.
  6. **Check `wp_head()` and `wp_footer()`**: Ensure your theme's `header.php` has `<?php wp_head(); ?>` just before `</head>` and `footer.php` has `<?php wp_footer(); ?>` just before `</body>`. Without these, WordPress cannot properly inject scripts and styles.

Addressing jQuery No-Conflict Mode

WordPress loads jQuery in "no-conflict mode" to prevent it from clashing with other JavaScript libraries that might also use the `$` shorthand. This is a common point of confusion for developers new to WordPress. If you see "`$` is not defined" in your console, this is almost certainly the cause.

Instead of `$` for jQuery selections, you must use the full `jQuery` keyword. The most robust way to handle this is to wrap your code in an immediately invoked function expression (IIFE) that passes the `jQuery` object as `$`.

  1. **Option 1: Use `jQuery` Instead of `$`**: Simply replace all instances of `$` with `jQuery` in your JavaScript code. For example, instead of `$('.my-class').hide();`, use `jQuery('.my-class').hide();`.
  2. **Option 2: Wrap Your Code in an IIFE (Recommended)**: This allows you to continue using the `$` shorthand within your script without causing conflicts. This is the cleaner approach for larger scripts.
  3. ```javascript (function($) { // Your JavaScript code here, using $ as shorthand for jQuery $(document).ready(function() { $('.my-button').on('click', function() { alert('Button clicked!'); }); }); })(jQuery); ```
  4. Place this wrapped code directly into your `my-script.js` file. The `(jQuery)` at the end passes the `jQuery` object into your function, mapping it to the local `$` variable, making your code safe and functional within the WordPress environment.

Ensuring JavaScript is Loaded at the Right Time (Header vs. Footer)

The timing of when your JavaScript loads can significantly impact its functionality and your site's performance. Generally, loading scripts in the footer is preferred.

When you call `wp_enqueue_script()`, the last parameter (a boolean) determines the load location:

`wp_enqueue_script('handle', 'src', array(), 'version', true);`

- If `true` (recommended), the script will load just before the closing `</body>` tag (via `wp_footer()`). This allows the HTML and CSS to load first, making your site appear faster and preventing scripts from trying to manipulate elements that haven't rendered yet.

- If `false` (or omitted), the script will load in the `<head>` section (via `wp_head()`). This is only necessary for scripts that *must* execute before the page content, such as certain analytics trackers or scripts that define global variables used throughout the page. Be cautious, as this can block rendering.

Verification and Final Checks

After implementing fixes, it's crucial to verify that your JavaScript is now running correctly. A quick check in the browser console is usually sufficient.

To ensure everything is working:

1. **Clear Caches**: If you have a caching plugin (e.g., WP Super Cache, WP Rocket) or server-level caching, clear it completely. Your browser might also be serving an old version, so perform a hard refresh (Ctrl+Shift+R or Cmd+Shift+R).

2. **Check the Console Again**: Reload your page and open the browser's developer console. Confirm there are no new JavaScript errors related to your script.

3. **Test Functionality**: Actively interact with the elements your JavaScript is supposed to affect. Click buttons, open modals, or observe animations. If they respond as intended, your JavaScript is likely executing successfully.

4. **Code Inspection**: Review the generated HTML source code one last time to ensure your script is present, has the correct path, and is loading in the expected location (header or footer).

By following these systematic steps, you can effectively diagnose and resolve common issues that prevent WordPress JavaScript from running in your theme, leading to a more interactive and functional website.

Frequently asked questions

Why does my WordPress JavaScript work on some pages but not others?
This often points to a conditional loading issue; your enqueueing function might only be triggered on specific page templates or conditions. Ensure `wp_enqueue_script()` is added to the `wp_enqueue_scripts` action hook without restrictive conditional tags unless intentionally desired for specific pages.
Can I use external JavaScript libraries in my WordPress theme?
Yes, you can enqueue external JavaScript libraries using `wp_enqueue_script()`, providing the full URL to the CDN or hosted file as the source. Remember to declare any dependencies correctly and use the `jQuery` no-conflict wrapper if applicable.
My theme was built with Themify; how do I add custom JavaScript?
Even with themes generated by tools like Themify, the best practice for adding custom JavaScript is still through the `functions.php` file using `wp_enqueue_script()`. This ensures your code integrates seamlessly and remains compatible with future theme updates, preventing direct modification of core theme files.
What's the difference between `wp_enqueue_script()` and directly linking in `header.php`?
`wp_enqueue_script()` is the WordPress-approved method that handles dependencies, versioning, and prevents conflicts, integrating seamlessly with the WordPress loading process. Directly linking in `header.php` bypasses these benefits, often leading to conflicts, poor performance, and broken functionality, making it a bad practice.

Try it in minutes — first conversion free

Themify is the fastest way to turn any live webpage into an installable WordPress theme (.zip). No coding, no rebuilding, no design handoff. Runs 100% locally in your browser.

No credit card required · 14-day money-back guarantee

Chrome browser logoAdd to Chrome — 1 free conversion