Adding a script to the top of the head

I need to add a script to the very top of the head section. I tried the solution listed here (Adding Script to Head Section of All Pages) and it worked to get my script into the head, but it was below many other scripts. This is for a cookie banner that I need to load before any other scripts load or set cookies. How can I make sure that it loads first? Thank you!

Hello @Albarella,

Thanks for writing to us

To load the script at the very top of the Head section you need to add the priority in the action hook.

$priority
(int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
Default value: 10

So you need to update the code and the updated code would be like this

add_action('wp_head', 'add_header_code',  -100);
function add_header_code(){
?>
    <!-- Add the Stripe code here -->

    <?php
};

Please note that the code provided above serves as a guide only and is to help you in getting started so implementing it and maintaining the code will be out of our support scope and for further maintenance for new possible versions of the theme that may cause the customization to break, you will need to hire a developer or you can subscribe to One where customization questions are answered.

Thanks for understanding

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.