Site logo

Archived topic

Need Help with Hooks

11 replies · Started by Alan on June 18, 2020

Viewing posts 1–12 of 12

I created the following hook in elements following the documentation provided. The hook adds a TrustPilot script to the head so that a widget can be displayed anywhere on the website. The widget is displaying properly, but there is an error code above the header that you can see on the url. Can you help me troubleshoot?

add_action( 'wp_head',arc_insert_into_wp_head );
function arc_insert_into_wp_head() { ?>
<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End TrustBox script -->
<?php }

According to the instructions provided by TrustPilot, I am supposed to add the code between <head> and </head> as close to the top of the page as possible. You can see here https://trstp.lt/9ZAxQcNqE.

I created a snippet using the Snippets plugin, and after adding the code as you suggested, and saving a draft of the snippet, I get an error indicating that "e is undefined" see here https://savacaystage.wpengine.com/wp-content/uploads/2020/06/TrustPilot-Widget-Snippet-Error.jpg

As a general rule, what is the best way to add code between <head> and </head>?

Please let me know your thoughts.

Thanks,

David,

That is the way that I originally did it but got an error message at the top of the header. Leo suggested that I use a snippet for the script, which also produced an error message. Can you look at my code above (#1333401) to see if you see anything wrong with it?

Thanks,

Can you paste the code here again -but before submitting the reply highlight the text and click the Code Button so we don't lose its formatting

David:

See code here...

add_action( 'wp_head','arc_insert_into_wp_head' );  
function arc_insert_into_wp_head() { ?> 
    
<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End TrustBox script -->

<?php }

You can see that the widget is appearing on the page, but there is an error code above the header on all pages https://savacaystage.wpengine.com/

That code is fine if you add it directly to a Code Snippet - NOT if you add it a Hook Element.

If you want to use a Hook Element just add:

<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End TrustBox script -->

And make sure you select the WP_Head hook.

The only code you need to add to the Hook Element is this:

<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End TrustBox script -->

David:

I get it now...all of the "add-action" language was unnecessary...the error message disappeared.

You solved my problem. Thanks!!

Totally - the Hook Element writes the add_action code and all the other conditional stuff for you :)

Glad you got it resolved.

This archived topic is closed to new replies.