Having trouble trying to add script in the header.php file - I need to include script for a live chat function but keep getting error message!

Hi, I have very limited experience of adding in scripts - I just usually stick to plugins - but the Live Chat that our client wants to use has a script that needs to be added to the header.php file, before the closing tag. I’ve tried to insert it into 2 different places and keep getting the same error message:

syntax error, unexpected ‘<’

I have no idea how I’m going to get this to work!

I’ve included a screen grab for your reference. The website I’m trying to do this on is: https://www.xpertcubes.com/

Thanks!

Hi There @core365

Thanks for writing in! To add a script into your head tag, first you need to setup a child theme and activate it by following this guide (https://theme.co/apex/forum/t/setup-how-to-setup-child-themes/57). Then you can use the following code snippet and add it into your child theme’s functions.php file.

 /*Adding a code inside the <head> tag*/
function my_custom_script() { ?>

<!-- YOUR CODE HERE -->

 <?php }

 add_action( 'wp_head', 'my_custom_script', 999 );

More example about wp_head() function here.

In case you need to place a code before the closing body tag, you can follow the example below.

  /*Adding a code before closing body tag </body> */
function my_custom_script() { ?>

<!-- YOUR CODE HERE -->

 <?php }

 add_action( 'wp_footer', 'my_custom_script', 999 );

More example about wp_footer() function here.

Hope that helps.

I’ve just done it, thanks! It was much easier than I thought it would be!

Thanks for your help!

You’re most welcome!

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