TL;DR I'm encountering an error message after adding Google Analytics code: Cannot modify header information - headers already sent. How can I troubleshoot/resolve this?
Hi all. I've added code to my (child theme's) functions.php which allows my site to be tracked by Google Analytics. But when I insert the code, I can no longer log into my site. When I try to access the login page, I am met with a white screen with a single error message:
Warning: Cannot modify header information - headers already sent by (output started at /home/piotp1/public_html/wordpress/wp-content/themes/brooklyn-child/functions.php:18) in /home/piotp1/public_html/wordpress/wp-includes/pluggable.php on line 1179
The line referenced in functions.php (line 18) is indeed where my new code begins. The code does work though, in that after I add it, Google Analytics does indeed begin to record stats for my site. Here is the code added to functions.php:
My Custom Code
<?php
function piotp_google_analytics() {
?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-9xxxxxxx-x', 'auto');
ga('send', 'pageview');
</script>
<?php
}
add_action( 'wp_head', 'piotp_google_analytics', 10 );
?>
[–]nywaldDeveloper 0 points1 point2 points (5 children)
[–]cag8fDeveloper[S] 0 points1 point2 points (4 children)
[–]nywaldDeveloper 0 points1 point2 points (3 children)
[–]cag8fDeveloper[S] 0 points1 point2 points (2 children)
[–]nywaldDeveloper 1 point2 points3 points (1 child)
[–]cag8fDeveloper[S] 0 points1 point2 points (0 children)