• Resolved satrya

    (@satrya)


    Hi there,
    So, I am using forminator_custom_form_submit_errors hook to send the form data to a custom endpoint. But, I just realized it also run when user click on Save Draft button. Is there a way to prevent this? I just want to run the function when user click the submit button. Here’s the sample code.

    /**
     * Send the form data to API
     */
    add_action(
        'forminator_custom_form_submit_errors',
        function ($submit_errors, $form_id, $field_data_array) {
            if ($form_id != FORMID) {
                return $submit_errors;
            }
    
            // API key & Endpoint
            $endpoint = 'ENDPOINT_URL';
    
            // Form data
            $data = array_column($field_data_array, 'value', 'name');
    
            $body = [
                'name' => $data['text-1'] ? esc_attr($data['text-1']) : '',
            ];
    
            // JSON encode data
            $body = wp_json_encode($body);
    
            // Send the data to the API
            $response = wp_remote_post($endpoint, [
                'body'        => $body,
                'headers'     => [
                    'Content-Type' => 'application/json',
                ],
                'timeout'     => 60,
                'redirection' => 5,
                'blocking'    => true,
                'httpversion' => '1.0',
                'data_format' => 'body',
            ]);
    
            if ($response['response']['code'] !== 200) {
                $submit_errors[]['submit'] = 'API Error!';
                $GLOBALS['api_error'] = true; // for custom message.
            } else {
                $GLOBALS['api_error'] = false;
            }
    
            return $submit_errors;
        },
        15,
        3
    );

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @satrya

    I hope you’re well today!

    You are right, this hook is used also if draft is saved.

    There’s no “alternative” but you should be able to overcome it with a slight modification:

    try adding this

    $save_draft = isset( $_POST['save_draft'] ) ? $_POST['save_draft'] : '';
    	if ( $save_draft ) {
    		$submit_errors = '';
    		return $submit_errors;
    	}

    directly below this part of your code

    if ($form_id != FORMID) {
         return $submit_errors;
    }

    Best regards,
    Adam

    Thread Starter satrya

    (@satrya)

    Great, it works perfectly. Thank you Adam @wpmudev-support8

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Run forminator_custom_form_submit_errors hook only for submit?’ is closed to new replies.