Troubleshooting Common Script Insertion Issues with the WPCode Plugin
Content
If you're using the WPCode plugin (formerly Insert Headers and Footers) to add custom scripts to your WordPress site, you might occasionally run into problems where the code doesn't execute, causes errors, or doesn't appear at all. This guide covers the most frequent issues users encounter and provides step-by-step solutions to get your scripts working correctly.
1. Scripts Not Executing or Displaying as Text
A very common issue is pasting JavaScript code without the proper <script> tags. The plugin offers different code types, and using the wrong one is a typical mistake.
- Why it happens: The plugin's JavaScript code type expects pure JavaScript without
<script>tags. The HTML code type is for any code that includes HTML tags. - The fix: If your code snippet already includes
<script>tags, you must select the HTML code type. If it is pure JavaScript without tags, use the JavaScript code type.
2. Script is Missing from the Page Source
You've added a script, but when you view your site's source code, it's nowhere to be found.
- Why it happens: This is often caused by aggressive caching or asset optimization plugins. These tools combine, minify, or delay scripts to improve performance, which can sometimes remove or break them.
- The fix: Temporarily clear all your site and browser caches. Then, try temporarily disabling any caching, CSS/JS optimization, or CDN settings. If the script appears, you know the optimization plugin is the culprit. You can then reconfigure it to exclude the specific script from optimization.
3. JavaScript Errors Breaking Other Scripts
An error from one script, often visible in the browser's console (F12), can prevent other scripts on the page from running.
- Why it happens: The script might be trying to access a page element (like
document.querySelector(...)) that doesn't exist yet or at all on the page. It could also be a conflict with another script or theme. - The fix: Open your browser's developer console (F12) and check for any red error messages. The error will usually point to the problematic line of code. Ensure your script is only loaded on pages where the required elements exist. You can use the plugin's built-in Insert Logic to control where a snippet runs. Also, try loading the script in the footer to ensure the DOM is fully loaded before it executes.
4. Script Works on Desktop But Not Mobile
Your code functions perfectly on a desktop browser but fails to work on mobile devices.
- Why it happens: This can occur if your site uses a separate mobile cache, a plugin that serves a different mobile version (like AMP), or if there is conditional logic that only runs on mobile.
- The fix: Check if you have any plugins that create a separate mobile experience or cache. Disable them temporarily to test. Also, use your mobile browser's inspection tools to check for any JavaScript errors that are specific to the mobile view.
5. Incorrect Script Placement
You need a script to load in a very specific location, such as right after the <head> tag, but it appears lower down.
- Why it happens: The WPCode plugin uses standard WordPress hooks like
wp_headto insert code. The exact placement within the<head>section is influenced by the priority set for that hook and what other themes/plugins are doing. - The fix: For a snippet, you can edit it and change the Priority number in the Insertion panel. A lower number (e.g., 1) means it will execute earlier and likely be placed higher up. However, note that any elements hardcoded directly in your theme's
header.phpfile will still appear above code inserted viawp_head.
6. jQuery Code Not Working
You've added a jQuery script, but nothing happens.
- Why it happens: Your script may be trying to run before the jQuery library itself has been loaded onto the page, or jQuery may not be loaded on your site at all.
- The fix: First, ensure your theme or another plugin loads jQuery. If it doesn't, you will need to enqueue jQuery first. You can do this by creating a separate PHP snippet in WPCode to load it. Secondly, make sure your custom jQuery script is set to load in the Footer to ensure it runs after jQuery is available.
General Troubleshooting Steps
If you're still stuck, follow this general process:
- Check the Code Type: Double-check that you've selected the correct code type (HTML or JavaScript) for your snippet.
- Disable Caching: Temporarily disable all caching and optimization plugins to rule them out.
- Check for Conflicts: Temporarily switch to a default WordPress theme (like Twenty Twenty-Four) and disable all other plugins except WPCode. If the script works, reactivate your theme and plugins one by one to find the conflict.
- Inspect the Console: Always use your browser's developer console (F12) to check for JavaScript errors. This is the most valuable tool for diagnosing script problems.
By methodically working through these common issues, you can resolve most problems related to inserting scripts with the WPCode plugin.
Related Support Threads Support
-
TypeError: document.querySelector(…) is null admin-bar.jshttps://wordpress.org/support/topic/typeerror-document-queryselector-is-null-admin-bar-js/
-
Javascript injection brokehttps://wordpress.org/support/topic/javascript-injection-broke/
-
jQuery works on desktop and tablet but not mobile browsershttps://wordpress.org/support/topic/jquery-works-on-desktop-and-tablet-but-not-mobile-browsers/
-
Script not showing in source codehttps://wordpress.org/support/topic/script-not-showing-in-source-code/
-
jQueryhttps://wordpress.org/support/topic/jquery-62/
-
Issue with uploading script to headerhttps://wordpress.org/support/topic/issue-with-uploading-script-to-header/
-
Can this be used to add external API to a page?https://wordpress.org/support/topic/can-this-be-used-to-add-external-api-to-a-page/
-
Does this enqueue?https://wordpress.org/support/topic/does-this-enqueue/
-
Script / Jquery footer not loading in Microsoft edgehttps://wordpress.org/support/topic/script-jquery-footer-not-loading-in-microsoft-edge/
-
Placement in thehttps://wordpress.org/support/topic/placement-in-the/
-
how to insert .js ?https://wordpress.org/support/topic/how-to-insert-js/
-
Problem with cooky policy in the footerhttps://wordpress.org/support/topic/problem-with-cooky-policy-in-the-footer/
-
Error while Inserting Codehttps://wordpress.org/support/topic/error-while-inserting-code/
-
Adding scripts in header removes home page elementshttps://wordpress.org/support/topic/adding-scripts-in-header-removes-home-page-elements/
-
Issue with head and body script. beginner helllphttps://wordpress.org/support/topic/issue-with-head-and-body-script-beginner-helllp/
-
HTML Code for inserting Amazon banner not workinghttps://wordpress.org/support/topic/html-code-for-inserting-amazon-banner-not-working/
-
Fire after consent / GDPRhttps://wordpress.org/support/topic/fire-after-consent-gdpr/
-
Insertar scripthttps://wordpress.org/support/topic/insertar-script/
-
Plugin doesn’t show up at all.https://wordpress.org/support/topic/plugin-doesnt-show-up-at-all/
-
JS/JQuery code in footer not workinghttps://wordpress.org/support/topic/js-jquery-code-in-footer-not-working/