Adding JS-code

Hello! I try to add some scripts to Custom JS in Theme Options but it doesn’t work…

Hi there,

Please double check and make sure that you do not include <script></script> in the custom JS panel.

In case the code still does not work, please double check that the code you have added is error free.

Hi! Is it nesessery not to include <script> tag? I have such kind of script:
<script charset="UTF-8" src="//cdn.sendpulse.com/js/push/86e49fb5f6681......" async></script>

Hi there,

It would be best if you load external scripts through the functions.php file of the child theme.

You can get the child theme from here and install and activate it.

Once installed you should be able to have the base functions.php file for the child theme.

Login through FTP and edit the functions.php file and add this code:

add_action('wp_head','custom_script');
function custom_script() {
?>
    <!-- Add the script code here -->
<?php
}

Hope this helps.

1 Like

Ok, it works! Thank you!
How can I add some scripts to the header, and some to the footer?

Hi there,

If you want to add more script in the head area, you can simple edit the code previously suggested and add the script in like this:

add_action('wp_head','custom_script');
function custom_script() {
?>
    <script></script>
    <script></script>
<?php
}

Do you mean adding some scripts before ? If so, you can use the wp_footer hook like this:

add_action('wp_footer','custom_footer_script');
function custom_footer_script() {
?>
    <script></script>
    <script></script>
<?php
}

Hope this helps.

I try to use this two pieces of code together but it crashes the site.

Hi there,

Would you please kindly share the code that you added in the functions.php or give us the URL/User/Pass of your WordPress dashboard using Secure Note to check the case.

I tested the code below:

add_action('wp_footer','custom_footer_script');
function custom_footer_script() {
?>
    <script src="/hello/"></script>
<?php
}

It worked, just change /hello/ with the URL of the script you have.

Thank you.

This code works fine, but my question was how to add one script to the header and another to the footer?

That question was answered previously.

It crashes your site maybe because of syntax error. This is why @christopher.amirian asked that you paste the whole code of your functions.php here so we could check why it crashes your site.

Anyway, another method would be to use a third party plugin to insert code in the header and footer. This way, you can avoid PHP syntax errors. Here’s a guide.

Thanks.

Thank you for advices!

I’ve found that is was my wrong - I didn’t change function name when copypasting the code…

Now everything works fine!

Glad to hear that.

Cheers!

Could I leave feature request here to add support for code without script-tag in Theme options?

Hey There,

Your feedback has been noted. We’ve added this as a feature request so it can be taken into consideration for future development.

Best Regards.

OK! Thanks a lot!

You’re most welcome.

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