Ad Code

Submitting Gravity Forms To 3rd Party Applications

If you develop websites in WordPress, there is a good chance that you have heard of Gravity Forms, a well-known plugin that allows you to implement forms very easily into your website.

How Does it Work?

Gravity Forms validates the data from the user, and stores it into the WordPress admin interface so you can log in and see who has submitted entries. 

What is the Downside?

The only downside to Gravity Forms is that by default it does not give you an option to have a second “action,” as you have to send your forms to 3rd party software. However, with some simple coding, this can be remedied!

Adding a Second Action

Start by creating your form within Gravity Forms and adding it to a page. Once it is implemented correctly, you can start the process of adding the second action. First, we need to get the relevant ID’s of the Gravity Form fields. Open up your functions.php file (/wp-content/themes/*your-theme*/functions.php) and scroll to the very bottom, and add this code:

add_action("gform_post_submission", "set_post_content", 10, 2);
 function set_post_content($entry, $form){
 //Gravity Forms has validated the data
 //Our Custom Form Submitted via PHP will go here
 // Lets get the IDs of the relevant fields and prepare an email message
 $message = print_r($entry, true);
 // In case any of our lines are larger than 70 characters, we should use wordwrap()
 $message = wordwrap($message, 70);
 // Send
 mail('mail@gmail.com', 'Getting the Gravity Form Field IDs', $message);
 }

The snippet above will hook into all forms being submitted through Gravity Forms after submission (Replace mail@gmail.com with your relevant email).  After putting this in your functions.php file, go ahead and fill out your form. Make sure to fill in ALL fields with relevant data so that we can determine which fields are associated with which IDs.

Sort out the relevant IDs to the relevant fields and write them down.  For example, my name is Travis Hoglund, so the first name field = 1.3, and the last name field = 1.6, etc.

Now that we know the field ID’s, we can implement a custom curl function to submit the data through PHP, just as it is sent through a browser. Below is the complete code – it might look like a lot, but I will explain it!

add_action("gform_post_submission", "set_post_content", 10, 2);
 function set_post_content($entry, $form){
 // Lets get the IDs of the relevant fields and prepare an email message
 //$message = print_r($entry, true);
 // In case any of our lines are larger than 70 characters, we should use wordwrap()
 //$message = wordwrap($message, 70);
 // Send
 //mail('mail@gmail.com', 'Getting the Gravity Form Field IDs', $message);

function post_to_url($url, $data) {
 $fields = '';
 foreach($data as $key => $value) {
 $fields .= $key . '=' . $value . '&';
 }
 rtrim($fields, '&');
 $post = curl_init();
 curl_setopt($post, CURLOPT_URL, $url);
 curl_setopt($post, CURLOPT_POST, count($data));
 curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
 curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($post);
 curl_close($post);
 }
 if($form["id"] == 8){//Join Our Mailing List
 $data = array(
 "first_name" =>     $entry["1.3"],
 "last_name" =>     $entry["1.6"],
 "title" =>         $entry["2"],
 "company" =>     $entry["3"],
 "street" =>         $entry["4.1"],
 "city" =>         $entry["4.3"],
 "state" =>         $entry["4.4"],
 "zip" =>         $entry["4.5"],
 "country" =>     $entry["4.6"],
 "website" =>     $entry["10"],
 "email" =>         $entry["5"],
 "phone" =>         $entry["6"],
 "industry" =>     $entry["7"],
 "description" => $entry["8"],
 "formName" => "join-mailing-list-98368402345"
 );
 post_to_url("https://thirdPartyApplication.php", $data);
 }
 }

Post a Comment

0 Comments