[Resolved] Need Help with Hooks

Home Forums Support [Resolved] Need Help with Hooks

Home Forums Support Need Help with Hooks

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1333401
    Alan

    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 }

    #1333460
    Leo
    Staff
    Customer Support

    Hi 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/

    #1333922
    Alan

    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,

    #1333940
    David
    Staff
    Customer Support

    Hi 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 the WP_head hook
    Then set your Display Rules to Entire Site.

    #1333974
    Alan

    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,

    #1334018
    David
    Staff
    Customer Support

    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

    #1334047
    Alan

    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/

    #1334055
    David
    Staff
    Customer Support

    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.

    #1334219
    Alan

    David:

    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/.

    #1334222
    David
    Staff
    Customer Support

    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 -->
    #1334251
    Alan

    David:

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

    You solved my problem. Thanks!!

    #1334261
    David
    Staff
    Customer Support

    Totally – the Hook Element writes the add_action code and all the other conditional stuff for you 🙂

    Glad you got it resolved.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.