davidhb21l6 Posted October 15, 2019 Share Posted October 15, 2019 Hi all, My company has a php site on wordpress that i’m trying to add google AD tracking to. There is a single header.php file for all pages so i’ve added a little piece of php to check what the current page is and to write out the appropriate event trigger. Now, on the live site, when i’m logged into word-press and run the page, I can see the triggers firing fine on Tag assistant, if i un-comment the alerts, they also fire. When i log out of word-press and hit the page, nothing happens at all. No alerts, no google triggers, nothing in the php is running? Any ideas whats going on ? <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'AW-1234567897'); <?php $url = $_SERVER['REQUEST_URI']; if ($url =="/thankyou/") { print " gtag('event', 'conversion', {'send_to': 'AW-1234567897/1234567897', 'value': 1.0, 'currency': 'GBP' }); "; print " gtag('event', 'conversion', {'send_to': 'AW-1234567897/1234567897'}); "; //Print "alert('OK');"; //Print "alert('" . $url . "');"; } ?> </script> tia Dave Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/ Share on other sites More sharing options...
requinix Posted October 15, 2019 Share Posted October 15, 2019 Is the ad code present in the page source? Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570710 Share on other sites More sharing options...
davidhb21l6 Posted October 15, 2019 Author Share Posted October 15, 2019 Thanks for the reply requinix, no. i cant see the google JS as its hidden in source. However, as a test, at the top of the header.php i added this code. <%php Print “alert(‘hello’);”; ?> when i’m logged into WP and hit the page, it triggers fine, (and i can see this in sourcecode) But if i log out of WP it doesn’t ( and not visible). just something very weird going on here i think. Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570711 Share on other sites More sharing options...
requinix Posted October 15, 2019 Share Posted October 15, 2019 If you don't see it in the source then you added the code to the wrong template. Or you accidentally put it inside some block that's only outputted for logged-in users. Either way, needs to go somewhere else. Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570713 Share on other sites More sharing options...
maxxd Posted October 15, 2019 Share Posted October 15, 2019 Also, <%php is not the correct opening tag. Use <?php. However, sounds like requinix is right - you're in an admin or restricted area of the template. What's the file name and location you're working with? Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570715 Share on other sites More sharing options...
davidhb21l6 Posted October 16, 2019 Author Share Posted October 16, 2019 thanks for the replies, interesting that there's outputted blocks for admin or users, i'll have to look into that. its the header.php file. should be present on every page,in fact it is.. The rest of the JS is coming out OK , such as the config tag, the initial async line etc.. its just the PHP code isnt showing ? <script async src="https://www.googletagmanager.com/gtag/js?id=AW-123123123"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'AW-123123123'); <?php $url = $_SERVER['REQUEST_URI']; if ($url =="/thankyou/") { print " gtag('event', 'conversion', {'send_to': 'AW-123123123/abcabcabcabc', 'value': 1.0, 'currency': 'GBP' }); "; print " gtag('event', 'conversion', {'send_to': 'AW-1123123123/asasasasasasasas'}); "; //Print "alert('OK');"; //Print "alert('" . $url . "');"; } ?> </script> very strange. Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570722 Share on other sites More sharing options...
requinix Posted October 16, 2019 Share Posted October 16, 2019 1. What is the URL in your browser's address bar? 2. What do you see when you View Source the page? Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570723 Share on other sites More sharing options...
cyberRobot Posted October 16, 2019 Share Posted October 16, 2019 (edited) 5 hours ago, davidhb21l6 said: its just the PHP code isnt showing ? Try adding a test line before your if statement. For example: <?php $url = $_SERVER['REQUEST_URI']; print 'HERE'; if ($url =="/thankyou/") { Does "HERE" appear on your pages when you're logged out...and in? For what it's worth, the PHP code in your if statement only gets executed when you're on the "/thankyou/" page. Is that page only displayed when you are logged in? Edited October 16, 2019 by cyberRobot added clarification Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570728 Share on other sites More sharing options...
maxxd Posted October 17, 2019 Share Posted October 17, 2019 18 hours ago, davidhb21l6 said: interesting that there's outputted blocks for admin or users, i'll have to look into that. its the header.php file. should be present on every page,in fact it is.. The rest of the JS is coming out OK , such as the config tag, the initial async line etc.. Depending on what theme you're using, it's very possible that that exact code is duplicated across multiple templates, so you could be in the wrong file without knowing it. WordPress doesn't exactly require - or even in a lot of ways promote - good practices when it comes to PHP and coding for the web. And some of the Automattic plugins are the worst offenders (WooCommerce, I'm looking at you), so don't assume it's good just because it came from the same company as WP or it's got a lot of stars. Quote Link to comment https://forums.phpfreaks.com/topic/309376-php-only-works-when-i%E2%80%99m-logged-into-wordpress/#findComment-1570759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.