[Forminator Pro] Forminator Custom Webhook

Now we are comparing Forminator with our current tool: Elementor Forms.

Using Elementor forms, we can create a custom webhook to route and post data to our custom PHP function for processing.

How can we setup custom webhook or custom ajax post using Forminator?

GOAL: Setup simple contact form, when user clicks submit, all POST data is submitted to our custom PHP function, where we can process the data directly.

  • Adam Czajczyk
    • Support Gorilla

    Hi VirtualArtists

    I hope you’re well today and thank you for your question!

    There are multiple action hooks in Forminator (you can do a search on “do_action” on source code to find them all, along with comments) and I think this might be useful for you:

    /**
    * Action called after full form submit
    *
    * @since 1.0.2
    *
    * @param int $form_id - the form id
    * @param array $response - the post response
    */
    do_action( 'forminator_custom_form_after_handle_submit', $form_id, $response );

    [see “/library/modules/custom-forms/front/front-action.php” file in plugin folder around line 128]

    Additionally/alternatively this filter might also be helpful (you’ll find it in the same file, slightly “higher” in the code):

    /**
    * Filter submit response
    *
    * @since 1.0.2
    *
    * @param array $response - the post response
    * @param int $form_id - the form id
    *
    * @return array $response
    */
    $response = apply_filters( 'forminator_custom_form_submit_response', $response, $form_id );

    They also both give you form_id and there’s an API that could be used to retrieve data:

    https://premium.wpmudev.org/docs/wpmu-dev-plugins/forminator-api-docs/

    Best regards,

    Adam

  • SoftArt
    • The Incredible Code Injector

    Hi – Thanks for the info, a couple of quick questions :

    1) Is there a way to add HOOK or FILTER – but Without Changing Source Code? Maybe we could let Forminator operate as normal BUT add an ADDITIONAL Hook to also ping our function on submit , so our code could also grab the Post Data, and process separately.

    2) After quick glance of Forminator API, I see many functions, but I do not see any function that will return the actual form “data”.

    Which API function would return all the “data” collected by a form?

  • Adam Czajczyk
    • Support Gorilla

    Hello VirtualArtists

    1) Is there a way to add HOOK or FILTER – but Without Changing Source Code? Maybe we could let Forminator operate as normal BUT add an ADDITIONAL Hook to also ping our function on submit , so our code could also grab the Post Data, and process separately.

    None of that requires changing Forminator’s source code. The “do_action()” means that you can “hook to it”. So in your code you just add a function similar to this

    add_action( 'forminator_custom_form_after_handle_submit', 'my_custom_function_doing_something_with_form_data', 10, 2 );

    function my_custom_function_doing_something_with_form_data( $form_id, $response ) {

    // in $form_id you got an ID of the form just submitted so you can use API do get data
    // and then to what you want/need with it

    }

    2) After quick glance of Forminator API, I see many functions, but I do not see any function that will return the actual form “data”.

    Which API function would return all the “data” collected by a form?

    Actually… you’re right. There are two function two get either all entries (submissions) of the form – assuming they are saved in the database – or a specific one but to fetch specific one, you need to know submission ID. Theoretically, you can assume that it’s the last (most recent) one but that’ still gives some small chance of getting wrong data if there’ll be another submission fast after the one you’re handling.

    I’m sorry about incorrect information, I should have checked the docs again before posting that. I’m pretty sure though that there’s another way for finding this so I’ve asked our developers for consultation and we’ll update you here as soon as we got some advice on this from them.

    Please keep an eye on this ticket for further information.

    Best regards,

    Adam

  • SoftArt
    • The Incredible Code Injector

    Hi – Thanks for the detailed info above :slight_smile:

    Based on your advice above, I created the following test function:

    function sc_sa_forminator_webhook( $form_id, $response ) {
    $_sDebugMsg = "Forminator-Webhook-Create-New-Ticket";
    sc_sa_zendesk_dev($_sDebugMsg);
    }
    add_action( 'forminator_custom_form_after_handle_submit', 'sc_sa_forminator_webhook', 10, 2 );

    The “add_action” code above is not working – it never fires :slight_frown:

    After Forminator submission, it never fires the function:

    sc_sa_forminator_webhook

    Are we missing something, or missing some settings in the test form?

    How to get the “add_action” above to fire test function correctly?

    Please Advise

  • Adam Czajczyk
    • Support Gorilla

    Hi VirtualArtists

    Thanks for response!

    As I mentioned previously, I asked our developers for consultation and am awaiting their response. It seems that they are a bit “loaded” with work so there might be slight delay on their end but either I or one of my colleagues will update you here as soon as we got advice from them on the solution.

    Best regards,

    Adam

  • Panos
    • SLS

    Hi there VirtualArtists !

    Forminator uses different hooks for Ajax submissions and Page Reload submissions. When the form’s Behaviour > Submission Behaviour > Method is set to Ajax it will trigger the forminator_custom_form_before_save_entry hook, if it is set to Page Reload it will trigger the forminator_custom_form_before_handle_submit. You can cover both with same callback :

    function sc_sa_forminator_webhook() {

    $post_data = $_POST;
    error_log('Forminator's post_data:: ' . print_r( $post_data,true ) );

    }

    // When Form's Submission Behaviour is set to Ajax
    add_action( 'forminator_custom_form_before_save_entry', 'sc_sa_forminator_webhook' );
    // When Form's Submission Behaviour is set to Page Reload
    add_action( 'forminator_custom_form_before_handle_submit', 'sc_sa_forminator_webhook' );

    Those are both before the handle_form() is called, so at that point the submitted data hasn’t been processed and stored yet. If you want to run your code after that, you can use

    forminator_custom_form_after_save_entry

    and

    forminator_custom_form_after_handle_submit

    accordingly.

    Hope this helps :slight_smile:

    Kind regards!

  • SoftArt
    • The Incredible Code Injector

    Hi Panos – Many thanks for the above code.

    We are now getting data from both of your new webhook sample codes above.

    From the POST data, we can extract our custom data, and other useful data including:

    [form_id] , and [page_id]

    Question: From the POST data example you gave above, how can we get “submission_id” ?

  • Adam Czajczyk
    • Support Gorilla

    Hi SoftArt

    The POST data contains only form content so there’s no “submission_id” (in terms of API that’d be an “entry_id” though).

    The “response” (which you can also read with a slight modification of Panos’ code) also doesn’t include that ID, even if it’s called on _after_save_entry/_after_handle_submit action so there seems to be no direct simple way to get that.

    In theory, if you’re calling your code on “forminator_custom_form_after_save_entry” hook, you could probably use “count_entries” API method and the number (or number minus 1 as I’m not sure if they are numbered starting from 1 or from 0) would in most cases be that submission but that’s something I mentioned previously and doesn’t seem quite reliable.

    I asked Panos to lend us a hand on this again so let’s see what he'[ll say :slight_smile:

    Best regards,

    Adam

  • Panos
    • SLS

    Hey SoftArt !

    In order to get the Entry ID ( by that I mean inserted record’s id from db table, if that is what you mean by submission_id ) none of the above will work. There is a more appropriate hook you can use for that:

    add_action( 'forminator_custom_form_submit_before_set_fields', function( $entry, $form_id, $field_data_array ){

    //Form Id :
    //$form_id

    // Entry Id (ID of new record in wp_frmt_form_entry DB table):
    // $entry_id = entry->entry_id

    }, 20, 3 );

    You can get the entry id with :

    $entry_id = entry->entry_id

    and loop through $field_data_array to get all fields names and values.

    Kind regards!

  • SoftArt
    • The Incredible Code Injector

    Greetings – Many thanks for the last thread above, it seems this may be exactly what we need.

    We will study all your code above, but for now this issue may be solved.

    Over time, we hope your Forminator can match feature-for-feature with Gravity Forms :slight_smile:

    Please keep developing the Forminator, so we can stay with your great form builder.

    Great Product – Great Support :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.