How to add script inside header? And keeping it there after update?

Hi

I have this trustpilot jv script, which need to be inside the <head></head> section.

<script async type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js"></script>

and some html code somewhere on the site:

<div class="trustpilot-widget" data-locale="da-DK" data-template-id="539ad60defb9600b94d7df2c" data-businessunit-id="4ee79077000064000511d535" data-style-height="500px" data-style-width="100%" data-stars="1,2,3,4,5"></div>

So I put it there, and usually it worked, but every time I update the theme it disappears from there. and now when I try to put the code inside the head section, the widget wont even appear anymore.

Is there anywhere where I can put it, so it stays there?
It is an important piece of code which creates a widget for user references & I want to have on the site.

I hope you can help?

Cheers
Vlad

Of course, every custom code is removed when you update the theme. That’s why you need to use a custom CSS plugin or a child theme or a custom PHP plugin :slight_smile:

You can try to install Code Snippets or some other custom PHP plugin and add this:


function trustpilot_script() {
    echo '<script async type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js"></script>' . "\n";
}
add_action( 'wp_head', 'trustpilot_script' ); 

Okay, now I see the point of using child themes! :wink:

But it worked with the code snippets plugin!

Thnaks a lot! Vlad

I have a similar though, different question about HOW TO ADD SCRIPT INSIDE HEADER?

I can’t find the HEAD SECTION in the Sydney theme via the WP dashboard. Please direct me so I find where to put my script?

Thank you!
Natalie