admin管理员组文章数量:1323502
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.
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.
Share Improve this question edited Sep 2, 2020 at 16:48 Scott Paterson asked Sep 2, 2020 at 6:40 Scott PatersonScott Paterson 12 bronze badges 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. – Jacob Peattie Commented Sep 2, 2020 at 7:47
- Thanks @JacobPeattie - I just added more details to my post. – Scott Paterson Commented Sep 2, 2020 at 16:14
1 Answer
Reset to default 0First 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;
}
本文标签: pluginsSharing varible between two addactions
版权声明:本文标题:plugins - Sharing varible between two add_actions 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141817a2422612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论