Add custom script to blog page

Hello - how do I add a custom script to the blog page?

This is the script that I would like to appear on my blog page only:

Summary

Thanks for your help!

Hello @ccwadsworth,

Thanks for asking. :slight_smile:

You can take a look at SOGO Add Script to Individual Pages Header Footer plugin to add script specific page like blog. Please use following URL to download the plugin.

Thanks.

Thanks for your response and the link.
I was hoping to do this without adding another plugin…
Is that possible?
Appreciate your help, thank you!

Hi,

In that case, you may add it using wp-footer hook

Add the code below in your child theme’s functions.php

function add_my_script() {
?>
<script>
  window.sugarwod_options = {
    id: "dAtRc5gEz6",
    color: "#f79646",
    position: "bottom-right"
}
</script>

<script src="//cdn.sugarwod.com/plugin/v1/sugarwod-plugin.js" async></script>

<?php
}
add_action( 'wp_footer', 'add_my_script' );

Additional Info:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_footer

Hope this helps

Thank you! Will this show up on my blog page only?

Hi @ccwadsworth,

It will be displayed everywhere, you can change it to this to make sure it’s only displayed on blog page.

function add_my_script() {
if ( is_home() ) : ?>
<script>
  window.sugarwod_options = {
    id: "dAtRc5gEz6",
    color: "#f79646",
    position: "bottom-right"
}
</script>

<script src="//cdn.sugarwod.com/plugin/v1/sugarwod-plugin.js" async></script>

<?php endif;
}
add_action( 'wp_footer', 'add_my_script' );

Hope this helps.

Thanks Rad — but it’s not showing up on my blog page… should the if statement be “is_blog” ?
It isn’t showing up on my home page either, by the way…
Appreciate your help!
Thank you!

Hello @ccwadsworth,

The code should have work and display the code in your blog index.

is_home() will return true/false if the page is your blog index. There isn’t is_blog() condition.

I have check all the site urls associated with your licenses. I am not sure I have looked into the correct url. To assist you with this issue, we’ll first need you to provide us with your URL. This is to ensure that we can provide you with a tailored answer to your situation.

Please let us know how it goes.

Thanks RueNel
The site license is for cynergycrossfit.com, the page is https://www.cynergycrossfit.com/blog/
I have the script loaded on the contact page so you can see how it works: https://www.cynergycrossfit.com/contact/
An orange button appears in the lower right corner.
Thanks for your help!

Hello @ccwadsworth,

Thank you for providing the site information. As I have checked it, I found out that you have installed a caching plugin WP Super Cache. Always keep in mind that after making any site changes, you must clear your plugin cache before testing your site. This can cause the changes to not take place on the front end. It will eventually show up when the cache regenerates, but it’s not ideal if you’re looking to see your changes immediately. Caching plugins are best to turn on only when you’ve finished building the site.

Hope this helps. Kindly let us know.

Hi RueNel
Thank you for your response. Yes, I am aware of the caching plugin and used it after I made the changes. I am still unable to see the script on the page.
I deleted cache again this morning and checked the page on 3 different browsers and still don’t see the updates.
Thanks so much for your help, I appreciate it!

Hi @ccwadsworth,

Please activate your child theme first, the reason it’s not working is because it’s not active.

Thanks!

Rad, thank you for pointing that out. I have no idea why the child theme wasn’t activated. I never used the main theme… the script is now working. I appreciate your help, thank you very much!

Glad it’s working now, cheers! And you’re most welcome!

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