Header and Footer plugin - Add php code



  • 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



  • 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



  • @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.



  • 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


Log in to reply