• Resolved airzona

    (@airzona)


    Hi,

    I am trying to send an email when a web hook for a form submission is triggered (I know I can do it through forminator but I need it done through a webhook for another reason).

    Not sure if I am doing it correctly. In functions.php I have added the the following:

    function my_form_function($module_id) {
        $to = '[email protected]';
        $subject = 'The subject';
        $body = 'The email body content';
        $headers = array('Content-Type: text/html; charset=UTF-8');
    
        wp_mail( $to, $subject, $body, $headers );
    }
    
    add_action('forminator_form_before_handle_submit','my_form_function', 10, 232 );

    I don’t get any errors but the email never gets sent, tested the function on its own and it does work just not via a form submission. FYI, my form id is 232 and I put the priority to 10, I believe I am setting that right?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @airzona

    Hope you are doing fine.

    Based on previous code examples from our development team, the hooks triggered on submitting the form is the following:

    add_action ( 'forminator_form_after_handle_submit', 'my_form_submit', 10, 2 );
    add_action( 'forminator_form_after_save_entry', 'my_form_submit', 10, 2 ); 
    
    function my_form_submit( $form_id, $response ) { 
    
        if ( $response && is_array( $response ) ) {
             if ( $response['success'] ) { 
                if ( $form_id == 232 ) { 
                    $entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id ); 
                  // Add the email delivery code here.....
    
                } 
            } 
        } 
    }

    This line of code was included in case you need to retrieve any values from the latest entry:

    $entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id );

    Hope this information helps as a reference.

    Kind regards

    Luis

    Thread Starter airzona

    (@airzona)

    Luis,

    Thanks for the quick reply! Is there any reason to worry that if there are a lot of form submissions that they will not match the current logged in user?

    In this function I need to also update the users meta data here as well. I just worry using the function”get_latest_entry_by_form_id”, would get the wrong one potentially if a bunch were submitted at the same time?

    Hope this makes sense, thanks!

    Thread Starter airzona

    (@airzona)

    Also, what could work is if I could add the user id to the form data on submission. Not sure if that is possible?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @airzona

    Thank you for response!

    Is there any reason to worry that if there are a lot of form submissions that they will not match the current logged in user?

    There shouldn’t be such issues (at least we didn’t encounter any) as there’s little chance that two or more submission would happen at the very same exact time up to fractions of the second but I do understand your concerns and there is another way.

    Below I’m sharing a slightly updated code:

    add_action ( 'forminator_form_after_handle_submit', 'my_form_submit', 10, 2 );
    add_action( 'forminator_form_after_save_entry', 'my_form_submit', 10, 2 ); 
    
    function my_form_submit( $form_id, $response ) { 
    
    	$form_ids = array( 232, 2688 ); // IDs of the forms this should work with
    	
    
        if ( $response && is_array( $response ) ) {
             if ( $response['success'] ) { 
    			if ( in_array( $form_id, $form_ids ) ) {
    		 
    				$entry = Forminator_CForm_Front_Action::$prepared_data;
                  // Add the email delivery code here.....
    
                } 
            } 
        } 
    }

    The main change here is replacing the “get_latest_entry…” by this:

    $entry = Forminator_CForm_Front_Action::$prepared_data;

    The difference here is that this way the date is actually always that very specific data of the particular submission – there is no risk of submissions messing up in this case at all.

    Additionally, I also made a small change where you can define more than a single forms if needed, by putting form IDs in this line

    $form_ids = array( 232, 2688 ); // IDs of the forms this should work with

    Also, what could work is if I could add the user id to the form data on submission. Not sure if that is possible?

    You simply need to add additional field to the form. Field should be of type “hidden” and its “Default value (optional)” option should be set to “User ID”.

    This way user ID for logged-in users will be included automatically in submission in that hidden field and you can easily take it from the $entry variable (array) in the code.

    Best regards,
    Adam

    Thread Starter airzona

    (@airzona)

    Thanks so much for the help. You guys are awesome!
    Is there a need to call both webhooks instead of just the forminator_form_after_save_entry:

    add_action ( 'forminator_form_after_handle_submit', 'my_form_submit', 10, 2 );
    add_action( 'forminator_form_after_save_entry', 'my_form_submit', 10, 2 ); 


    One last question… where does $prepared_data come from and what is this variable?

    ie:

    $entry = Forminator_CForm_Front_Action::$prepared_data;

    • This reply was modified 1 year, 8 months ago by airzona.
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @airzona

    As far as I remember correctly, those both hooks are needed to make sure that code works regardless of whether form is set to be loaded and/or submitted via AJAX or not. But you can test it by simply removing one of them, then another – and checking in which cases with which AJAX settings it works for you.

    One last question… where does $prepared_data come from and what is this variable?

    It’s internally set by Forminator code (by “Forminator_Front_Action” class) when when form is submitted.

    In general:

    – when you submit a form – any form – a set of POST data is submitted to certain target URL.

    – a function (or rather, a method of Forminator_Front_Action class) in Forminator code takes that POST data, sanitizes it and “organizes” in a nice array for later use

    Basically, it’s kind of like using $_POST[‘something’] to read POST data but with a few advantages: you do not need to know exactly what data to read, you don’t need to sanitize the data for security reasons (as it’s already done, unlike with reading raw POST data) and you have all the submitted data (and some other information) right away in a single array variable.

    Best regards,
    Adam

    Thread Starter airzona

    (@airzona)

    Thanks for your help Adam, you are amazing!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Issues with forminator web hooks’ is closed to new replies.