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.