0

I'd like to share a variable between two add_actions. Currently I'm trying to use a global. However it doesn't work. This simple example below illustrates the problem. In this example, test_before_send_mail() runs before test_get_form_post_callback().


$test = '123';

add_action('wpcf7_before_send_mail', 'test_before_send_mail');
function test_before_send_mail() {
     global $test;
     $test = '456';
}


add_action('wp_ajax_test_get_form_post', 'test_get_form_post_callback',1);
add_action('wp_ajax_nopriv_test_get_form_post', 'test_get_form_post_callback',1);
function test_get_form_post_callback() {
     global $test;
     echo $test; ($test is equal to 123 still, when it should be equal to 456.)
}

I have tried changing the add_action priorities levels and tried passing variables via the function directly. I think this may has something to do with wpcf7_before_send_mail specifically. Anyone have any ideas how to get this working?


Edit: To add more about what I am trying to do - When Contact Form 7 is submitted it runs (wpcf7_before_send_mail). I'm using an add_action to hook into that. There are variables set at this point that I need to use later.

After the Contact Form 7 form is submitted, I am using JavaScript to force the form to redirect. During this redirect, I need to access those variables from before, so I am using an add_action Ajax hook.

I can't use cookies or sessions either. I've tried both of those approaches and it either fails in some browsers or on some servers with session caching.

2
  • This won't work. Your AJAX callback is going to be a separate request to the form submission action. You will need to specify exactly what you're trying to do for anyone to be able to offer an appropriate solution. Commented Sep 2, 2020 at 7:47
  • Thanks @JacobPeattie - I just added more details to my post. Commented Sep 2, 2020 at 16:14

1 Answer 1

0

First of all, the order in which you use add_action is not relevant. The only thing it does is keep a tab of actions to undertake once the hook is encountered. Only when there are several functions on the same hook, the priority of the added action becomes relevant (read more).

So, if your global variable isn't changed, this means that the hook changing it (wpcf7_before_send_mail) is called AFTER the hook where you test if it's been changed (wp_ajax_test_get_form_post). Or maybe the changing hook is not called at all. You can change the latter by echoing something inside test_before_send_mail(). If nothing is printed, the hook is not called, if there is, the hook is called too late.

Depending on what you're looking to achieve the easiest solution would be to attach test_get_form_post_callback to wpcf7_before_send_mail as well, and use priorities to assure the right execution order.

If you are in command of both test_before_send_mail() and test_get_form_post_callback the cleanest solution would be to include a filter hook in one of the functions, eliminating the need for a global variable. Like this:

function wpse374144_main ($args) {
  ... do stuff
  $var = something;
  $var = apply_filters ('wpse374144_main_filter', $var);
  }

 add_filter ('wpse374144_main_filter','wpse374144_main_callback',10,1);

 function wpse374144_main_callback ($var) {
   ... do stuff
   return $var;
   }
2
  • Thanks for the reply @cjbj - I just tried your add_filter suggestive with no luck. I added more details to my post which should help explain what I am trying to do. Commented Sep 2, 2020 at 16:17
  • Right. Looks like you're trying to hand over a variable from one PHP session to the other. That's not possible. You'll have to store it somewhere. You could try a custom query var, but I don't know if that'll play with cf7.
    – cjbj
    Commented Sep 3, 2020 at 10:10

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.