最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - I don't understand how add_action and do_action work in tandem. The former executes the code already...what is d

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

The 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论