title: Can one hook in wp_head inside other hook?
body: <p>I have to add some scripts in the head part of our website whenever a login or registration of a user was successful. For the login and register forms we use the Ultimate Member plugin, which has a hook method  <code>um_user_login</code> and <code>um_user_register</code> which I though I could use for that.</p>
  
  <p>I.e. if a user login was successful, do an <code>head.append(...)</code> in JavaScript.</p>
  
  <p>However I am a NodeJS and Java developer and completely new to Wordpress plugin programming. I can't find a suitable solution so far for this easy sounding task.</p>
  
  <p>My idea was to hook at the <code>um_user_login</code> action and do something like:</p>
  
  <pre><code>add_action('um_user_login', 'add_tracking_after_login', 10);
  
  function add_tracking_after_login() {
      add_action('wp_head', function() {
          echo '&lt;script type="text/javascript" src="my_script.js"&gt;&lt;/script&gt;';
      });
  }
  </code></pre>
  
  <p>However, I have tested this with different logging, but it seems like the hook function to wp_head is never executed (the function <code>add_tracking_after_login</code> is, as I have tested this with logging as well). So I think, I cannot add an action to wp_head this way.</p>
  
  <p>Is there any other way to achieve the requested functionality? Or am I doing the hooking wrong as I am new to WordPress plugin programming?</p>
  
  <p><br/></p>
  
  <p><strong>Edit</strong>: To keep confusion low, this is just one of several tasks the client has requested. I also need to react on successful registrations and on successful sent emails with our contact form. In these cases I can't find an analogous workaround as in the answer below of Harry SM. I need to react on success events of the plugins we use.</p>
  
  <p>Thanks in advance and best regards</p>