3

I've been looking for a few days in the stack overflow here but I can't find the answer to my problem. I'm trying to add the Cookie Yes script, to insert the cookie banner on my WordPress site. The script must be loaded at the beginning of the tag before any other script, because it must preventively block the other Facebook, analytics, etc. scripts. I tried adding this to my child-theme's function.php file:

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

But the plugins that add the other scripts are always inserted before the one I want to add. Thank you very much.

Here the real example:

in function.php file

/* add script at the top of head tag */
add_action('wp_head', 'add_top_head_script',0);

function cookieyes_script() {
    echo '<!-- Start cookieyes banner --> <script id="cookieyes" type="text/javascript" src="https://cdn-cookieyes.com/client_data/754d136f697eda5270dfe657/script.js"></script> <!-- End cookieyes banner -->';
}

page source code

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • 1
    What you've pasted here is a `function` that isn't called. Can you share how this information is being output to the DOM? Are you hooking an event? Where is this function being called, exactly? – esqew Jan 12 '22 at 16:56
  • Sorry, I took the action for granted. My bad. – Carlo Alberto Jan 13 '22 at 05:13

2 Answers2

2

One way you might be able to get this done is with the wp_head action with 0 as the priority...

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

One issue here is that if another script uses this same priority of 0 the hook that is registered first will be higher. That said the default priority for add_action is 10 so probably safe here.

mikerojas
  • 2,238
  • 1
  • 4
  • 7
  • 1
    Yes I tried to give priority 0 (someone here on Stack Overflow also suggests negative numbers -100) but unfortunately, the other scripts are still inserted first. – Carlo Alberto Jan 13 '22 at 04:59
  • Can you share more about how those other scripts are being added? Could me and others here better troubleshoot. – mikerojas Jan 13 '22 at 15:39
  • Ok added the real example – Carlo Alberto Jan 14 '22 at 08:14
  • Ok added the real example. It's the only thing I am able to share. I think that all the scripts that comes before the cookie script are injected with gtag manager. Unfortunately, I don't have access to the google tag manager account. – Carlo Alberto Jan 14 '22 at 09:57
-1

If you want to add scripts on top of wp_head you can use the priority variable in add_action. 0 is supposed to be the highest priority, but if something already has a 0 priority, you can use a negative one. Here is an example in your case:

/* add script at the top of head tag */
function cookieyes_script() {
    echo '<!-- Start cookieyes banner --> <script id="cookieyes" type="text/javascript" src="https://cdn-cookieyes.com/client_data/754d136f697eda5270dfe657/script.js"></script> <!-- End cookieyes banner -->';
}

add_action('wp_head', 'add_top_head_script', -100);

Source: How to add a script on top of wp_head in Wordpress

pomaaa
  • 596
  • 1
  • 4
  • 18