Digital experiences for all disciplines
New Landing › How can we help? › Themeforest Theme Support › Uplift › pasting code to child theme
New Landing › How can we help? › Themeforest Theme Support › Uplift › pasting code to child theme
- This topic has 11 replies, 3 voices, and was last updated 7 years by David Martin – Support.
-
Posted in: Uplift
-
February 27, 2017 at 6:25 am #315174
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> tagI have child theme installed, but not sure what to do from now…
Thanks!
February 27, 2017 at 9:58 am #315184Hi,
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
February 27, 2017 at 11:54 am #315208This reply has been marked as private.February 27, 2017 at 11:57 am #315211You 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
February 27, 2017 at 1:34 pm #315240This reply has been marked as private.February 27, 2017 at 1:41 pm #315245Yes, 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
withsf_before_page_container
.February 27, 2017 at 2:06 pm #315259This reply has been marked as private.February 27, 2017 at 3:12 pm #315278If you added your FTP details I’ll check the code.
Cheers
February 28, 2017 at 7:48 am #315402This reply has been marked as private.February 28, 2017 at 10:12 am #315410Sorry I should of asked you to also confirm the staging URL?
Thanks.
February 28, 2017 at 10:39 am #315415This reply has been marked as private.March 1, 2017 at 3:13 pm #315630Hi,
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' );
-
Posted in: Uplift
You must be logged in to reply to this topic.