New Landing How can we help? Themeforest Theme Support Uplift pasting code to child theme

Viewing 12 posts - 1 through 12 (of 12 total)
  • Posted in: Uplift
  • #315174
    micheal_w
    Member
    Post count: 498

    Hi guys,

    Apart from installing a 3rd party plugin, what is the easiest way to install code to

    1. as high in the <head> of the page as possible
    2. immediately after the opening <body> tag

    I have child theme installed, but not sure what to do from now…

    Thanks!

    #315184
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    Can you explain us exactly what you are trying to do?

    WordPress has a lot of hooks and filters that allow to inject code in specific parts of the html inside a simple function in the child theme.

    -Rui

    #315208
    micheal_w
    Member
    Post count: 498
    This reply has been marked as private.
    #315211
    David Martin – Support
    Moderator
    Post count: 20834

    You can hook into the WP action wp_head.

    To hook into after opening body, you would use this action: sf_before_page_container.

    Examples: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

    #315240
    micheal_w
    Member
    Post count: 498
    This reply has been marked as private.
    #315245
    David Martin – Support
    Moderator
    Post count: 20834

    Yes, that’s exactly right. However, you may need to open and close PHP tags depending on your functions content. Ex:

    // Add scripts to wp_head()
    function child_theme_head_script() { ?>
    	<!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-T6JBN6W');</script>
    <!-- End Google Tag Manager -->
    <?php }
    add_action( 'wp_head', 'child_theme_head_script' );

    For the second, create another function just like above, named differently though. Replace wp_head with sf_before_page_container.

    #315259
    micheal_w
    Member
    Post count: 498
    This reply has been marked as private.
    #315278
    David Martin – Support
    Moderator
    Post count: 20834

    If you added your FTP details I’ll check the code.

    Cheers

    #315402
    micheal_w
    Member
    Post count: 498
    This reply has been marked as private.
    #315410
    David Martin – Support
    Moderator
    Post count: 20834

    Sorry I should of asked you to also confirm the staging URL?

    Thanks.

    #315415
    micheal_w
    Member
    Post count: 498
    This reply has been marked as private.
    #315630
    David Martin – Support
    Moderator
    Post count: 20834

    Hi,

    I have added this without issue:

    // Before body scripts
    function sf_child_theme_before_body_script() { ?>
    	<!-- Google Tag Manager (noscript) -->
    	<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T6JBN6W" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    	<!-- End Google Tag Manager (noscript) -->
    <?php 
    }
    add_action( 'sf_before_page_container', 'sf_child_theme_before_body_script' );
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.