How to add code within the <body> element of my website?

Hello,

How can I add code within the element of my website?
Could you please indicate me which file I should modify for this (in the child theme I guess)?

Thanks,
Antoine

Hi Antoine,

Thanks for writing in! First you need to setup a child theme and activate it by following our guide here (https://theme.co/apex/forum/t/setup-how-to-setup-child-themes/57).

Then you can add the following code into your child theme’s functions.php file and integrate your code into that.

// Integrate a Code after body tag

function my_custom_code() { 
  // Add your custom code here
}

add_action('x_before_site_begin', 'my_custom_code');

Likewise, there are certain hooks provided withing the X/Pro themes which you can utilize. Please find more information from the following link (https://theme.co/apex/forum/t/customizations-actions-and-filters-in-x-pro/208).

Also I would suggest you to check the customization best practices guide from here (https://theme.co/apex/forum/t/customizations-best-practices/205).

Hope that helps.

Thank you for the explanation.

  1. How about adding code adding code in the head tag?

  2. Moreover, I noticed that the code I have in the functions.php of my child theme is not working anymore. I have for example some translation that used to work, and they are not working anymore. How can I fix the problem, can you please help me with this?

Thanks,
Antoine

Hi @Lecoqdigital,

  1. You can use the plugin Insert Headers and Footers instead for adding code to <body> and/or <head> section of the site.

  2. What code did you add on your child theme’s functions.php file? Please provide us login credentials wp-login and FTP so we can take a closer look.

Thanks,

  1. Is there no way to add the code within the function.php file in the same way as for the body tag? I’d prefer not to have to use a plugin.

  2. I checked and they are correct.

Hi @Lecoqdigital,

It seems you’ve switched to the Pro parent theme. Could you please double check?

Please try with this code:

function adding_scripts_to_head(){
	?>
	<!-- Replace this with your code -->
	<?php
}
add_action( 'wp_head', 'adding_scripts_to_head' );

Hope it helps :slight_smile:

I’m so stupid for not checking this :slight_smile:
Thanks for the code and your help !

If you need anything else, feel free to create another ticket.

I do not manage to make it work. I tried to add code to the body tad adding the following code in the function.php of the child theme. Is the code wrong? Or I am missing something?

Note: it doesn’t display correctly in the forum so I am not sure if you do have the code correctly.

<script type="text/javascript">
      const date = new Date();
      const day = date.getDay();
      const hour = date.getHours();
      const minute = date.getMinutes();
      console.log(hour);
      console.log(minute);

      if (day >= 1 && day <= 5 && hour >= 8 && hour <= 15) {
      
        <script type="text/javascript">
(function(d, src, c) { var t=d.scripts[d.scripts.length - 1],s=d.createElement('script');s.id='la_x2s6df8d';s.async=true;s.src=src;s.onload=s.onreadystatechange=function(){var rs=this.readyState;if(rs&&(rs!='complete')&&(rs!='loaded')){return;}c(this);};t.parentElement.insertBefore(s,t.nextSibling);})(document,
'https://aide.neowebsite.io/scripts/track.js',
function(e){ LiveAgent.createButton('9f7eb493', e); });
</script>
      
      } else if (day >= 1 && day <= 5 && hour == 16 && minute <= 30) {
      
        <script type="text/javascript">
(function(d, src, c) { var t=d.scripts[d.scripts.length - 1],s=d.createElement('script');s.id='la_x2s6df8d';s.async=true;s.src=src;s.onload=s.onreadystatechange=function(){var rs=this.readyState;if(rs&&(rs!='complete')&&(rs!='loaded')){return;}c(this);};t.parentElement.insertBefore(s,t.nextSibling);})(document,
'https://aide.neowebsite.io/scripts/track.js',
function(e){ LiveAgent.createButton('9f7eb493', e); });
</script>
      
      } else {
      
        <!-- 
          your Quriobot URL (copy-paste that from the embed code) should be inserted on the script.src line below 
        -->
        
        let script = document.createElement('script');
        script.src = "  https://quriobot.com/qb/widget/MY12wr8lP2rd8ARO/wKYPWr60pKbd0px4";
        document.getElementsByTagName('body')[0].appendChild(script);
      }

Hello Antoine,

The code given by @Thai should work.

The code you have shared is a Javascript. If this does not work for you, I would recommend that you contact the creator of this code or the site that you got this code from. There might be a syntax error with your Javascript. Regretfully we cannot provide more information about this Javascript code as we are not familiar with this code.

Thank you for your understanding.

Thanks, I perfectly understand.

When you say my code is Javascript, does it mean I should not add it to the function.php? I am just trying to understand if I do the right thing or not…

Thanks,
Antoine

Hello Antoine,

Yes, your code is a javascript. You cannot add any javascript code in the functions.php file. Unless otherwise you have wrapped or included the javascript code inside a PHP function like this:

function adding_scripts_to_head(){
	?>

	<script type="text/javascript">
           // some javascript code
        </script>
 
	<?php
}
add_action( 'wp_head', 'adding_scripts_to_head' );

Hope this helps.

Thank you for the explanation. I will see if I manage to get something to work.

You’re welcome!
We’re glad we were able to help you out.

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