Back to Community

How to Fix Common Issues When Adding Scripts to Your Header with Code Snippets

6 threads Sep 7, 2025 PluginCode snippets

Content

Adding third-party scripts like Facebook Pixel, Google Analytics, or cookie consent banners is a common task for WordPress site owners. The Code Snippets plugin is a popular tool for this job, but users often run into a few specific issues. This guide covers the most common problems and their solutions.

The Common Problem: Scripts Not Loading Correctly

Based on community reports, the primary issue is that a script added via the plugin doesn't work as expected. The symptoms can vary:

  • The script fails to load, showing an error like e is undefined.
  • The script loads, but a Content Security Policy (CSP) error appears in the browser console.
  • The script loads, but it runs in the wrong order, causing conflicts (a common issue with cookie consent banners that must block other scripts).
  • The script tag is not visible in the page's HTML source code at all.

Why This Happens

These problems usually occur for one of a few key reasons:

  1. Incorrect Code Format: Pasting raw HTML/JavaScript directly into a snippet without wrapping it in the proper PHP function.
  2. Execution Order: WordPress has a specific load order. Simply changing a snippet's "priority" may not be enough to make it run before other plugins output their code.
  3. External Conflicts: Other plugins, especially those that optimize, minify, or cache JavaScript, can interfere with how and when your snippet loads.
  4. Server Security Policies: Your web host or a security plugin might have a Content Security Policy (CSP) that blocks the external source of your script.

Solutions to Try

1. Wrap Your Script in the Correct PHP Code

You cannot paste raw HTML or JavaScript directly into a snippet and expect it to work. You must use a WordPress hook to output the code in the correct location. For scripts that belong in the <head> section, the standard method is to use the wp_head hook.

Example for a Cookie Banner:

add_action( 'wp_head', function () { ?>
<!-- Start cookieyes banner -->
<script id="cookieyes" type="text/javascript" src="https://cdn-cookieyes.com/client_data/YOUR_ID/script.js"></script>
<!-- End cookieyes banner -->
<?php } );

Important: Ensure you are creating a "PHP Snippet" and not an "HTML," "CSS," or "JavaScript" snippet when using this method.

2. Adjust the Execution Priority (For Load Order)

If you need your snippet to run earlier than other code attached to the same hook (e.g., to ensure a cookie banner loads before analytics), you can adjust the priority number. A lower number means a higher priority (executes earlier). The default priority is 10.

Example to Run Early:

add_action( 'wp_head', 'my_early_script', 1 );
function my_early_script() { ?>
    <!-- Your script code here -->
<?php }

Note: The Code Snippets team notes that this controls the order snippets are loaded by the plugin, but it may not guarantee a script runs before code output by other plugins, as they also use priorities.

3. Check for Plugin and Theme Conflicts

If your snippet appears correct but still doesn't work, a conflict is likely. To test this:

  1. Temporarily disable any caching, optimization, or minification plugins (e.g., WP Rocket, W3 Total Cache, Autoptimize). Clear all caches after deactivating.
  2. Check if the snippet works. If it does, you'll need to reconfigure your optimization plugin to exclude the script from being modified.
  3. If that doesn't help, switch to a default WordPress theme (like Twenty Twenty-Four) temporarily to rule out a theme-specific conflict.

4. Investigate Content Security Policy (CSP) Errors

If your browser's console shows a CSP error, your server is blocking the script for security reasons. The solution is not to modify the snippet itself but to update your site's CSP header to allow the external domain.

  • This policy might be set by your web host (check your hosting control panel or contact them).
  • It might be set by a security plugin (e.g., Wordfence, Sucuri). Check the plugin's settings for "Content Security Policy" or similar and add the required domain (e.g., cdn-cookieyes.com) to the script-src directive.

When These Solutions Aren't Enough

For extremely strict requirements where a script must be the very first thing in the <head>, using the Code Snippets plugin with the wp_head hook may not be sufficient, as other themes and plugins can output code earlier. In these advanced cases, directly editing your theme's header.php file (in a child theme) is often the most reliable method, though it requires more technical skill.

By following these troubleshooting steps, you should be able to resolve most issues related to adding scripts to your site's header with the Code Snippets plugin.