0

I've tried reading the documentation and the definitions just elude me. I found this example that works here for my troubles -> https://stackoverflow.com/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;

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

  • Oh shoot ok. That would explain my confusion. So when plugin documentations mention actions they have available under do_action's. They are inherently hard coded already and what i'm supposed to do is hook onto it with add_action to give it my custom functionalities to it? That makes sense I suppose given that the documentation explicitly lists the do_action's under what circumstances they occur, so i'm more or less assigning to it an action. Yeah that makes a lot more sense. – Dennisio May 14 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.