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 -->';
}
One way you might be able to get this done is with the
wp_headaction with0as the priority...One issue here is that if another script uses this same priority of
0the hook that is registered first will be higher. That said the default priority foradd_actionis10so probably safe here.