Need variables for passing form info to Mailgun API

Below is the snippet I need modified along with the details:

<?php
/*
* I need this function to essentially make a remote request (POST) to a service URL from a specific form, passing along remapped form submission values
* -- 1) Using forminator action "forminator_custom_form_submit_before_set_fields"
* -- 2) Need it to -only- work on a specific form or forms
* -- 3) Remap form submission values to match the api values which are listed below
* -- 4) Don't need it to send an email, only sbmit post data to api as an option
*
* Using snippet from: https://www.billerickson.net/contact-form-integration/
*/
add_action( 'forminator_custom_form_submit_before_set_fields', 'the_forminator_mailgun_connector', 10, 4 );
function the_forminator_mailgun_connector( $entry, $form_id, $field_data_array ) {
$api_url = 'https://path/to/my/api.php';

//I need this only to work on a specific form or forms
$form_id = 5;
$to_array = false;

$body = array( //I don't know the correct way to get the specific form field value here from forminator
'name' => Forminator_API::get_form_field( $form_id, 'name-1', $to_array ), //?
'email' => $field_data_array[''][$field_id]['value'], //?
'zipcode' => $fields['value'], //?
);
$request = wp_remote_post( $api_url, array( 'body' => $body ) );

// Simple error handling
if ( is_wp_error( $request ) ) {
$msg = "There was an error trying to subscribe a user to the mailing list.n";
$msg .= 'Error returned: ' . $error = $request->get_error_message() . "nn";
$msg .= "Contact the user below about the error and see if they want to be added to the mailing list manually.n";
$msg .= $body['name'] . ' ' . $body['email'] . ' ' . $body['zipcode'];

wp_mail( get_bloginfo( 'admin_email' ), 'The Mailgun Subscribe Error', $msg );
}
}

  • Kostas
    • CTO

    Hi Chad ,

    I’ve altered your code a little bit to show you how this could be done. This method without doing a loop to map the array items into a different array is the easiest but it needs all the fields of the form to be as Required so their order won’t change as well as there won’t be an “empty” one.

    I’ve also added some local debug information that can be easily enabled by doing $debug_this = true; to help with the arrays and values needed.

    On my example I’m using a form with ID 46 so the $allowed_form_ids also accepts a 46 as well as the 5 from your code.

    You will of course need to update the $allowed_form_ids with the form IDs that you like as well as set up the correct values for your $body according to the array that you’re receiving in $field_data_array.

    The code:

    <?php

    add_action( 'forminator_custom_form_submit_before_set_fields', 'the_forminator_mailgun_connector', 15, 3 );
    function the_forminator_mailgun_connector( $entry, $form_id, $field_data_array ) {
    // Enabled Debugger, set to true or false.
    $debug_this = false;

    // Convert Form ID to int.
    $form_id = intval( $form_id );

    // Set the allowed Fomr IDs into an int array.
    $allowed_form_ids = array(
    5,
    46,
    );

    // A simple custom debugging script to output the needed information to be used in the $body later on.
    // This will output everything needed in debug.log if WP_DEBUG_LOG is enabled.
    if ( $debug_this ) {
    error_log( '+++++++++++++++++++++++++++++++++++++++++++++++' );
    error_log( 'Form ID: ' . $form_id );
    in_array( $form_id, $allowed_form_ids, true ) ? error_log( 'Form in Array: true' ) : error_log( 'Form in Array: false' );
    error_log( 'Field data: ' . print_r( $field_data_array, true ) );
    }

    // Only continue if the $form_id is within the allowed array
    if ( in_array( $form_id, $allowed_form_ids, true ) ) {

    $api_url = 'https://path/to/my/api.php';

    $body = array(
    'name' => $field_data_array[0]['value'],
    'email' => $field_data_array[1]['value'],
    'zipcode' => $field_data_array[2]['value']['zip'],
    );

    // A simple custom debugging script to output the needed information to be used in the $body later on.
    // This will output everything needed in debug.log if WP_DEBUG_LOG is enabled.
    if ( $debug_this ) {
    error_log( 'Request body data: ' . print_r( $body, true ) );
    error_log( '+++++++++++++++++++++++++++++++++++++++++++++++' );
    }

    $request = wp_remote_post( $api_url, array( 'body' => $body ) );

    // Simple error handling
    if ( is_wp_error( $request ) ) {
    $msg = "There was an error trying to subscribe a user to the mailing list.n";
    $msg .= 'Error returned: ' . $error = $request->get_error_message() . "nn";
    $msg .= "Contact the user below about the error and see if they want to be added to the mailing list manually.n";
    $msg .= $body['name'] . ' ' . $body['email'] . ' ' . $body['zipcode'];

    wp_mail( get_bloginfo( 'admin_email' ), 'The Mailgun Subscribe Error', $msg );
    }
    }
    }

    And this is what I have with the debugger enabled so you can see my $field_data_array and $body as well as it was mapped:

    [17-Apr-2019 12:39:42 UTC] +++++++++++++++++++++++++++++++++++++++++++++++
    [17-Apr-2019 12:39:42 UTC] Form ID: 46
    [17-Apr-2019 12:39:42 UTC] Form in Array: true
    [17-Apr-2019 12:39:42 UTC] Field data: Array
    (
    [0] => Array
    (
    [name] => name-1
    [value] => Some Name
    )

    [1] => Array
    (
    [name] => email-1
    [value] => some@email.test
    )

    [2] => Array
    (
    [name] => address-1
    [value] => Array
    (
    [zip] => 12345
    )

    )

    [3] => Array
    (
    [name] => _forminator_user_ip
    [value] => 127.0.0.1
    )

    )

    [17-Apr-2019 12:39:42 UTC] Request body data: Array
    (
    [name] => Some Name
    [email] => some@email.test
    [zipcode] => 12345
    )

    [17-Apr-2019 12:39:42 UTC] +++++++++++++++++++++++++++++++++++++++++++++++

    Tell me if you need any further help with this!

    Regards,

    Konstantinos