1

I have questions about executing add_action() in a Wordpress plugin. I was attempting to insert into the tag via the plugin i wrote.

I had this (which never ran, btw)

function tt_add_GA4_header() 
{
  $ga4code = get_option( '_ga4token' );
  // PHP vars are not globals, rather locals
  
  ?>
  <!-- for Google Analytics 4 -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo $ga4code; ?>"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', "<?php echo $ga4code;?>");
  </script>
  <?php
}

function tt_add_headers() 
{
  $header_libs = get_option( '_custom_headers' );

  ?>
  <?php if ( isset( $header_libs ) ) { echo $header_libs; } ?>
  <?php
}

function tt_activate() 
{
  add_action('wp_head', 'tt_add_GA4_header');
  add_action('wp_head', 'tt_add_headers');
}

register_activation_hook( __FILE__, 'tt_activate' );

Chrome's page source showed nothing of the GA4_header and other headers in tt_add_headers.

However, this worked - I placed these two add_action() functions

add_action('wp_head', 'tt_add_headers');
add_action('wp_head', 'tt_add_GA4_header');

outside the function tt_activate() together with register_activation_hook(). Chrome's Page source now displayed the GA4 header and tt_add_headers. For tt_add_headers, I had to surround the code with tags or the tt_add_headers will downshifted into the tag instead of the tag.

Anyone can provide pointers to educate me:

  1. why does add_action need to go into main plugin file to be activated instead of using the register_hook_activation()?
  2. why must tags be present in the $header_libs in order to be found in tag, else the $header_libs will be found in the tag?

1 Answer 1

1

why does add_action need to go into main plugin file to be activated instead of using the register_hook_activation()?

You've misunderstood how WordPress and PHP work. Any code that does not write to the database or a file is not persistent, and add_action() does not do either. Any time a WordPress page is requested by the browser the whole WordPress lifecycle runs. This includes loading and running plugins.

The way plugins work is that when they run, they can use add_action() or add_filter() to schedule code to run at later points during the page load. So when you use add_action() with wp_head you're telling WordPress "when you run the wp_head action, please run this code". This means that every time your plugin runs, you need to tell it to add your action, otherwise nothing will be added to wp_head.

The mistake you've made is that register_activation_hook() only runs whenever the plugin is activated. Any code in the activation hook will not run for subsequent requests.

why must tags be present in the $header_libs in order to be found in tag, else the $header_libs will be found in the tag?

This is just HTML. Any thing inside <head> must be in a valid tag, like <link>, <script>, <title> or <meta>. You can't just put text inside a <head> tag.

1
  • Thank you very much for this. This is my first time with Wordpress plugins. Your explanation is crystal. Jun 30, 2023 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.