• Hi all,
    I’ll start out by saying i’m not a used to php or word press dev. I’m a .net developer.

    My company has a php site on wordpress that i’m trying to add google 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

Viewing 13 replies - 1 through 13 (of 13 total)
  • @davehb21l6
    It sounds like you may be inside of a conditional checking if the user is logged in or possibly the user role. Do you see any kind of if statements surrounding your code? if(is_user_logged_in()) or if( current_user_can( ‘edit_post’, $post_id ) )
    etc. Anything like that?

    Thread Starter davehb21l6

    (@davehb21l6)

    Thanks for the reply wvfitzgerald

    no, nothing that i can see.
    in the php file, there is

    <html>
    <head>
       <meta...>
       <link css...>
    
       <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>
    
      <script> facebook pixel <script>
    
    </head>

    that’s why i was thinking that wp maybe doing something that i can’t see, like disabling the php.
    its just strange that it works fine, when i’m logged into wp, but then nothing when i log out.

    is there any conditional checking at WP level (outside of code) that i can check?

    Dion

    (@diondesigns)

    Your code checks for a URL that exactly matches /thankyou/. If will not match if the URL is missing the trailing slash, or if some sort of query string has been appended to the URL. Is it possible the URL is slightly different when logged in versus logged out?

    @davehb21l6
    to add to @diondesigns comment it is also a little fragile, give this snippet a try. Be sure to change $id to the page id of your thankyou page, you can find the id by going to the Thank You pages edit screen and looking in the browser’s address bar for something that looks like this: ?post=27 .

    <?php if (get_the_ID() == $id ): ?>
                <script>
                    Your js here;
                </script>
      <?php endif; ?>

    Be aware that if you are not using a child/custom theme this will be overwritten when your theme is updated.

    For future reference you will likely want to learn about wp_enqueue_scripts

    Thread Starter davehb21l6

    (@davehb21l6)

    thanks for the replies guys, i do appreciate it!

    the page always loads as /thankyou/ . (with the trailing slash)
    so i’m sure i have that part correct.

    just as a test, i added this to the top of my header.php file.
    <%php
    Print “alert(‘hello’);”;
    ?>

    when i’m logged into WP and hit the page, it triggers fine, but if i log out of WP it doesn’t.
    just something very weird going on here i think.

    @davehb21l6
    Did you try the snippet I posted above? Where the results the same?

    Dion

    (@diondesigns)

    If you display the HTML source of the page (Ctrl-U on most browsers) when logged in and out of WordPress, what do you see in the section where the PHP is located?

    Thread Starter davehb21l6

    (@davehb21l6)

    Hi Guys,
    i added this code to the top of my header.php file..

    <?php
    print “<script> alert(‘” . $id . “‘); </script>”;
    if (get_the_ID() == $id ) {
    print “<script> alert(‘working – ” . $id . “‘); </script>”;
    }
    ?>

    When i’m logged into WP, the first print (alert box) is triggered but its blank, so its not seeing the variable $id
    When i’m not logged into WP, nothing happens..

    because the $id isn’t returning a value the if statement is never going to be true.

    its like PHP isn’t even running when i’m logged out.

    Dave

    @davehb21l6
    Yes, the first would be empty because it has no way of determining the page id at that point, $id is just a placeholder in the example I gave you. $id would need to be changed to the actual page id on your thantkyou page, the instructions on how to find that are in my earlier post.

    If you just want to check if it is working your could add something like this:
    `<?php
    $id = get_the_ID();
    var_dump($id);
    ?>`

    Honestly, I think at this point it may be better to remove your code and follow this article on how to do this using a plugin:
    https://www.wpbeginner.com/wp-tutorials/how-to-easily-add-javascript-in-wordpress-pages-or-posts/

    Thread Starter davehb21l6

    (@davehb21l6)

    Hi wvfitzgerald ,
    Sorry, i made a response earlier and never actually posted it :/

    I buckled in the end and sort some help.
    it turns out that WP caching was causing some problems here, and also i was pointed to a better solution using WP variable.

    if ($post_slug == ‘thankyou’) {
    ‘ print code here.
    }

    And this works fine.

    Thanks for all your help guys!

    Dave

    Dion

    (@diondesigns)

     

    • This reply was modified 5 years, 1 month ago by Dion.
    Thread Starter davehb21l6

    (@davehb21l6)

    Hi Dion,
    There’s never a stupid question ??

    sorry, yes the header.php is inside a custom built theme. i should have mentioned that.
    Yes, when i log into domainname.com/wp-admin/ it allowed the php to run, but loggged out it never. i wonder if it was running a cached version when i wasn’t logged in.

    Sorry, i’m very new to word-press. i’ve developed in .net and iis for years, so its a whole new area having to learn php and Wp.

    Thanks
    Dave

    @davehb21l6
    Glad you got it worked out! Everyone is new at some point. ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘php only works when i’m logged into wordpress.’ is closed to new replies.