Need to place Amazon Onelink code in footer

Hey there. I’ve tried several things that are not working to get code properly into the footer, including:

add_action( 'wp_footer', 'add_footer_code', 9999 );

function add_footer_code()  { ?>
  <AMAZON SCRIPT>
<?php }

The amazon links are supposed to take you to the US or Canada depending on where you are. I added it to the functions.php of my child theme, but after option-command-U it has some other script after it that includes some custom CSS.

Hello @theresaladner,

Thanks for writing in!

If you are looking to add scripts in footer, you can add following code in child theme function.php file:

function third_party_tracking_code() { ?>

          <script>
           
          // Amazon tracking code.
          </script>

        <?php }

        add_action( 'wp_head', 'third_party_tracking_code' );

Replace // Amazon tracking code. with the tracking code.

Then add your code in Scripts in Header

  • Alternatively you also use following plugin to add the tracking code

Thanks.

So I tried adding the code in the function.php. Nothing worked and when I viewed the source for the page it said
!unexpected token

Hi @theresaladner,

I checked the front end of the website, and I see that the code that you have is an HTML code and not a Javascript code. The sample code that @Prasant suggested assumes that you want to add a Javascript code.

The solution is to remove the <script> and </script> tags, so that the HTML code will be a valid one. So, the sample code will be:

function third_party_tracking_code() { ?>
           
<!-- Remove this line completely and add the Amazon code instead. -->

 <?php }

add_action( 'wp_footer', 'third_party_tracking_code' );

I changed the code and replaced wp_head with wp_footer. The reason for this is that the first code added the Amazon code into the tag of HTML which is not the correct place if you use something other than Javascript code. So as you are using a simple HTML code, you need to add this code at the end of the tag, which is what the wp_footer hook does.

Thank you.

This is all getting a bit much so I added the Insert Header and Footer Plugin and put the Amazon code in the footer section.

It is still not working properly; what am I doing wrong?

Also, the Amazon tech I just spoke to said the code isn’t placed correctly in the footer. “oneTag should be installed on the footer of your website or just before the end of the tag.”

How do I fix that?

EDIT: I ended up inserting it into the parent theme footer.php and it works now.

Hello @theresaladner,

Thanks for updating the thread.

I am glad that you got it working. However, there’s one problem that you might face going forward when entering codes in parent theme. When the theme gets updated, it will also replace all the files and thereby it will remove the code that you have entered.

To learn more about benefits of child theme, please refer following post:

In the screenshot that you have shared, you only need to enter the script tag and the code under the same. Rest is not required. Please delete the rest of the codes and that should work.

Thanks.

I do have a child theme and I did put the script in the child theme function.php where it did not work.

Hi @theresaladner,

Would you mind providing the overall code that you added to the child theme’s functions.php? It should work there, and even with the use Insert Header and Footer Plugin should work too, except you shouldm’t add <?php wp_footer(); ?>.

Thanks!

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