Created
December 21, 2015 17:40
-
-
Save haydenbbickerton/1b519220f0c8d7b31a16 to your computer and use it in GitHub Desktop.
Ninja Forms to Improveit360 ELead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Made for use with Ninja Forms and Improveit360, | |
* should work with others with adjustment. | |
* | |
* Based on Alex Hagers Post | |
* http://www.alexhager.at/how-to-integrate-salesforce-in-contact-form-7/ | |
*/ | |
function ninja_forms_post() | |
{ | |
add_action('ninja_forms_post_process', 'post_to_elead'); | |
} | |
add_action('init', 'ninja_forms_post'); | |
function post_to_elead() | |
{ | |
/** | |
* Different forms have different IDs for the fields. | |
* So we'll use an array to map the IDs for each form. | |
*/ | |
$form_value_map = [ | |
6 =>[ // Ask A Question | |
'FirstName' => 35, | |
'LastName' => 36, | |
'Email' => 8, | |
'Phone' => 7, | |
'Comments' => 9 | |
], | |
11 =>[ // Free Estimate | |
'FirstName' => 13, | |
'LastName' => 14, | |
'Email' => 16, | |
'Phone' => 15, | |
'Comments' => 17 | |
], | |
12 =>[ // Get A Price | |
'FirstName' => 37, | |
'LastName' => 38, | |
'Email' => 21, | |
'Phone' => 20, | |
'Comments' => null | |
], | |
15 =>[ // Get A Price Sidebar | |
'FirstName' => 39, | |
'LastName' => 40, | |
'Email' => 29, | |
'Phone' => 28, | |
'Comments' => null | |
] | |
]; | |
global $ninja_forms_processing; | |
$form_id = $ninja_forms_processing->get_form_ID(); | |
//Get all the user submitted values | |
$all_fields = $ninja_forms_processing->get_all_fields(); | |
// Get the form keys... | |
$keys = $form_value_map[$form_id]; | |
// Now we'll get our values using the id from our keys | |
$first_name = $all_fields[$keys['FirstName']]; | |
$last_name = $all_fields[$keys['LastName']]; | |
$email = $all_fields[$keys['Email']]; | |
$phone = $all_fields[$keys['Phone']]; | |
// Some forms don't have comments, so we'll check for null in the map and make them blank | |
$comments = (isset($keys['Comments']) ? $all_fields[$keys['Comments']] : ''); | |
// We'll make an array that contains our values in {key}={value} format, for the URL | |
$post_items[] = 'FirstName=' . $first_name; | |
$post_items[] = 'LastName=' . $last_name; | |
$post_items[] = 'Email=' . $email; | |
$post_items[] = 'Phone1=' . preg_replace('/\D/', '', $phone); // Strip phone to only numbers | |
$post_items[] = 'Comments=' . $comments; | |
$post_items[] = 'Source=Website'; | |
if (!empty($first_name) && !empty($last_name) && !empty($email)) | |
{ | |
// Join our fields | |
$post_string = implode('&', $post_items); | |
// Base URL. We'll add the fields to the end of this. | |
// You can find yours by following the guide - http://improveit360.wikidot.com/elead-rest | |
$con_url = ''; | |
// Create a new cURL resource | |
$ch = curl_init(); | |
// Set the URL | |
curl_setopt($ch, CURLOPT_URL, $con_url); | |
// Set the method to POST | |
curl_setopt($ch, CURLOPT_POST, 1); | |
// Pass POST data | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); | |
// POST-IT© | |
curl_exec($ch); | |
// close cURL resource | |
curl_close($ch); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment