- This topic has 11 replies, 3 voices, and was last updated 3 years, 10 months ago by David.
-
AuthorPosts
-
June 18, 2020 at 5:20 pm #1333401Alan
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 }June 18, 2020 at 7:35 pm #1333460LeoStaffCustomer SupportHi there,
Are you adding that code in the hooks element?
That code should be added as a PHP snippet:
https://docs.generatepress.com/article/adding-php/June 19, 2020 at 5:07 am #1333922AlanAccording 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,
June 19, 2020 at 5:27 am #1333940DavidStaffCustomer SupportHi there,
simplest method is to use a Hook Element:
https://docs.generatepress.com/article/hooks-element-overview/
Just add the
<script>
to to the hook content and select theWP_head
hook
Then set your Display Rules to Entire Site.June 19, 2020 at 5:59 am #1333974AlanDavid,
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,
June 19, 2020 at 6:55 am #1334018DavidStaffCustomer SupportCan 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
June 19, 2020 at 7:24 am #1334047AlanDavid:
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/
June 19, 2020 at 7:34 am #1334055DavidStaffCustomer SupportThat 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.June 19, 2020 at 8:02 am #1334219AlanDavid:
The code from #1334047 is from a hook element that I created with wp_head selected. The script that you suggested I add is already there. You can see the entire element here https://savacaystage.wpengine.com/wp-content/uploads/2020/06/TrustPilot-Hook.jpg.
You can see the error message at the top of the header here https://savacaystage.wpengine.com/.
June 19, 2020 at 8:06 am #1334222DavidStaffCustomer SupportThe 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 -->
June 19, 2020 at 8:22 am #1334251AlanDavid:
I get it now…all of the “add-action” language was unnecessary…the error message disappeared.
You solved my problem. Thanks!!
June 19, 2020 at 8:26 am #1334261DavidStaffCustomer SupportTotally – the Hook Element writes the
add_action
code and all the other conditional stuff for you 🙂Glad you got it resolved.
-
AuthorPosts
- You must be logged in to reply to this topic.