Insert a script in <body>

Hello,

I need to install a live chat script on my website. They ask to insert the script before </body> How and where can I do it? Ideally not installing any extra plugins, I have too many of them already. Thanks.

@OlgaPollini,

Thanks for writing in! You can do this via a child theme by hooking into wp_footer via the child theme’s functions.php file. Here is information on setting up a child theme from our Knowledge Base:

Once you have that setup, within the child theme’s functions.php file, you could do something like the following:

function my_custom_scripts() {

  ?>

  <!-- Remove this comment and place scripts here -->

  <?php

}

add_action( 'wp_footer', 'my_custom_scripts', 10 );

As the snippet above says, you can replace the HTML comment in there with your <script> tags provided by the live chat developers and you should be good to go. Cheers!

Thanks but it did not help. Yes, I have a Child theme activated, I edited the functions file as you said but the live xhat does not appear on the website. U refreshed the pages and checked several pages.

@OlgaPollini, then you might need to consult with your live chat provider or double check that you’re outputting your script properly (e.g. within tags, in the right spot, et cetera). The WordPress community at large is built around making modifications to themes via these native hooks, and wp_footer is the most common hook to utilize for outputting any sort of custom, one-off scripts into a theme like this.

I personally tested the PHP snippet provided above myself before posting it, so I can confirm that it does indeed work.

I see. Is this kay like this? https://s.mail.ru/9Y5o/a1c8M6DFg

That appears to be correct, @OlgaPollini. Make sure the code is saved to the child theme and that you have activated the child theme. If you have done all of that, then at this time you would need to consult with the chat provider since all of your code is in the appropriate places and should be outputting properly as long as it’s saved and the child theme is active.

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