admin管理员组文章数量:1431420
I've tried reading the documentation and the definitions just elude me. I found this example that works here for my troubles ->
And while i'm happy that it works, I wish I understood why. I'm just extremely lucky that this code works out the box for my purposes
In my functions.php. I have the add_action and the function. The function does stuff. When I post my form, it works. Why?
I'm NOT using do_action anywhere, so what is it's purpose then and why is add_action working automatically where i'm just adding a hook to a custom function?
Also in the example, the do_action has more params passed. The add_action function has only one, yet i'm able to access it like an array with ease? Honestly I have no clue what is going on. This feels like magic and no attempt at reverse engineering is helping me make sense of this.
Any laymens way of explaining this? Below is reposted the code from the first answer of the question i'm linking at, for simplicity's sake.
do_action from the official plugin doc
do_action( 'wpforms_process_complete', $this->fields, $entry, $form_data, $entry_id );
add_action custom made?
add_action("wpforms_process_complete", 'function_save_custom_form_data');
function function_save_custom_form_data($params) {
foreach($params as $idx=>$item) {
$field_name = $item['name'];
$fiel_value = $item['value'];
// Do whatever you need
}
return true;
}
I've tried reading the documentation and the definitions just elude me. I found this example that works here for my troubles -> https://stackoverflow/questions/50286549/how-can-i-store-the-values-from-a-wpform-in-wordpress-to-mysql-database
And while i'm happy that it works, I wish I understood why. I'm just extremely lucky that this code works out the box for my purposes
In my functions.php. I have the add_action and the function. The function does stuff. When I post my form, it works. Why?
I'm NOT using do_action anywhere, so what is it's purpose then and why is add_action working automatically where i'm just adding a hook to a custom function?
Also in the example, the do_action has more params passed. The add_action function has only one, yet i'm able to access it like an array with ease? Honestly I have no clue what is going on. This feels like magic and no attempt at reverse engineering is helping me make sense of this.
Any laymens way of explaining this? Below is reposted the code from the first answer of the question i'm linking at, for simplicity's sake.
do_action from the official plugin doc
do_action( 'wpforms_process_complete', $this->fields, $entry, $form_data, $entry_id );
add_action custom made?
add_action("wpforms_process_complete", 'function_save_custom_form_data');
function function_save_custom_form_data($params) {
foreach($params as $idx=>$item) {
$field_name = $item['name'];
$fiel_value = $item['value'];
// Do whatever you need
}
return true;
}
Share
Improve this question
asked May 14, 2019 at 1:07
DennisioDennisio
151 silver badge5 bronze badges
2
- Possible duplicate of Difference between do_action and add_action – Nicolai Grossherr Commented May 14, 2019 at 2:35
- And similar: What do add_filters() and apply_filter() do? – Nicolai Grossherr Commented May 14, 2019 at 2:36
1 Answer
Reset to default 2The do_action()
function executes any code that's been hooked with add_action()
. The only reason your function_save_custom_form_data()
function runs is because the WP Forms plugin has added a call to do_action()
inside its code.
So what using do_action()
in a theme or plugin does is allow other plugins or themes to run code during its processes. In your example, whenever the form processing is completed, the WP Forms plugin runs do_action( 'wpforms_process_complete' )
. This doesn't do anything on its own, but it allows you to use add_action()
to run code at that point by using add_action()
.
Also in the example, the do_action has more params passed. The add_action function has only one, yet i'm able to access it like an array with ease?
In your callback function you're accessing $this->fields
as $params
(so it's not an appropriate name), which happens to be an array. You don't have any access to $entry
, $form_data
or $entry_id
. To get access to these you need to pass the number of arguments you accept to add_action
. For example, this allows you to access all 4 arguments:
add_action( 'wpforms_process_complete', 'function_save_custom_form_data', 10, 4 );
The last argument is the "accepted args" value. By setting it to 4 you're letting WordPress know that your function_save_custom_form_data()
function accepts 4 arguments, which you can now use like this:
function function_save_custom_form_data( $fields, $entry, $form_data, $entry_id ) {
Under the hood the 2nd arugment you're passing to add_action()
is a callback, and do_action()
just executes that callback.
本文标签:
版权声明:本文标题:hooks - I don't understand how add_action and do_action work in tandem. The former executes the code already...what is d 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745497854a2660890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论