Navigation

    Digital Liberation Forums

    • Register
    • Login
    • Search

    Header and Footer plugin - Add php code

    Header and Footer Scripts
    2
    4
    129
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Patrickre last edited by

      Hi there,
      I'm using your plugin "Header and Footer Scripts" to add tracking code to a certain thank you page on my website:

      <script>
      gtag('event', 'conversion', {
      'send_to': 'AW-XXXXX',
      'transaction_id': ''
      });
      </script>

      I would like to read out parameters from the URL of my thank you page (www.mypage.com/thank-you/?order_id=XYZ) by php and add them as a transaction_id:

      <?php echo get_query_var( 'order_id' );?>
      

      But when I add this to my tracking script, it doesn't echo the value but just writes it in plain text in the page source:

      <script>
        gtag('event', 'conversion', {
            'send_to': 'AW-XXXXX',
            'transaction_id': '<?php echo get_query_var( 'order_id' );?>'
        });
      </script>
      

      Is there a way to add this php code in your plugin?

      Thanks,
      Patrick

      anand 1 Reply Last reply Reply Quote
      • anand
        anand @Patrickre last edited by

        Hello @Patrickre

        You can't execute PHP using the plugin. You should probably add it to functions.php of your child theme. (Using IF ELSE statement you can easily execute PHP on the desired pages)

        Executing PHP within the plugin is not available for security reasons, as many people will eventually break their site.

        HTH

        P 1 Reply Last reply Reply Quote
        • P
          Patrickre @anand last edited by

          @anand Thanks for your quick reply. I already have a working solution to add it in functions.php

          But I don't really like that solution as the website has to run the if request for every page load instead of just adding the code for a single page (by using the plugin).

          Of course if the plugin doesn't execute php there doesn't seem to be any other way.

          1 Reply Last reply Reply Quote
          • anand
            anand last edited by

            There would not be any performance issue as such. But if you really want to execute the code on only one page then creating custom page template might be a solution.
            https://developer.wordpress.org/themes/template-files-section/page-template-files/

            Unfortunately, with the plugin it's not possible.

            PS: Imo, i would still suggest about using functions.php

            1 Reply Last reply Reply Quote
            • First post
              Last post