Gravity Forms: Submitting 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:
The snippet above will hook into all forms being submitted through Gravity Forms after submission (Replace firstname.lastname@example.org 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.
You should receive an email that looks similar to this:
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!
As you can see, I commented out the email code because we do not need this anymore. If you have access to the third party forms, you will want to use software like Firebug (or you can just view source) and get the name attribute of each field.
I used Firebug to grab this snippet from my third party form. You will notice that the form is submitting to https://thirdPartyApplication.php so that is the value that must go in your code. Below that is the third party code for the First Name Field which has a name of first_name, so that is what it MUST be in the above PHP code if you want the third party to grab your POST variables correctly.
Most forms use hidden fields to determine what form is being submitted, so make sure you include them in your PHP code as well. Here, the name and value are important:
As long as you make sure to include all of your form fields, use the correct IDs, and submit them with the relevant name attributes from the third party forms, you get the best of both worlds. Gravity Forms will validate your forms, save them to the admin interface, and your third party software will also receive them.
Happy Gravity Forms!
Travis Hoglund Zer0 to 5ive Senior Developer