Integrating Contact Form 7 with iContact
Here is another post on integrating Contact Form 7 WordPress plugin to an email marketing service which I did for my recent client – this time with iContact. There are few ways to use iContact API, but I chose to use send HTTP post with json encoding. A convenient built-in WordPress function makes it relatively straightforward.
Happy coding!
Register your App
First, you need to create and register an application for your iContact account. Follow the steps outlined here. You can create a sandbox account if you want to test the integration before you apply it to your active lists. In either case, you will need the following information:
- API Username (If using sandbox, this is a username for your sandbox account)
- Application ID
- Application Password
- API URL (you will see this under Account Information when you set your application password. It should look like this: https://app.icontact.com/icp/a/123456/c/78901/)
Find List ID
You need a numeric ID for the list that you want to put your contact into. To find this, go to your iContact Homepage, and in the Contact Lists, hover over the name of the list that you want, and get the link URL. The URL should look like this:
https://app.icontact.com/icp/core/mycontacts/contacts/view/?token=01a2bc34de567f89012a34567b8c9de01&clear=1&groupid=12345&ordercol=date&orderdir=1
The List ID is the numeric value after “groupid=”. For the dummy example above, the ID is 12345.
Create Contact Form
Using Contact Form 7 plugin, create your contact form. For integration purposes, you want to set up First Name, Last Name, and Email fields. For the example below, the names of those input fields are “your-first-name”, “your-last-name”, and “your-email”. Optionally, you can create a checkbox for opt-in.
The Code
Put the following code in your function.php file for your theme folder. Replace List ID, API URL, Application ID, Account Username, and Application Password with your own:
function wpcf7_send_to_icontact($cfdata) { // get the form content $formdata = $cfdata->posted_data; $contact_email = $formdata['your-email']; $contact_fname = $formdata['your-first-name']; $contact_lname = $formdata['your-last-name']; // set list ID for the forms $listId = '------List ID------'; //this is the base api post http url $post_url = '------API URL------'; $headers = array( 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Api-Version' => '2.0', 'Api-AppId' => '------Application ID------', // app id here 'Api-Username' => '------Account Userame------', // this is the log in username 'Api-Password' => '------Application Password', // this is the password for api app 'user-agent' => 'Contact Form 7 Integration' // this can be whatever you want ); $contact_data = json_encode(array(array( 'email' => $contact_email, 'firstName' => $contact_fname, 'lastName' => $contact_lname ))); $contact_args = array( 'body' => $contact_data, 'method' => 'POST', 'headers' => $headers, 'sslverify' => false, // Set SSL verify to false because of server issues. 'timeout' => 30 ); // http post to add contact to iContact $result = wp_remote_request($post_url.'contacts/', $contact_args); // get the contact ID from the result $result_body = json_decode(wp_remote_retrieve_body($result), true); $contact_result = $result_body['contacts']; $contact_full = $contact_result[0]; $contactId = $contact_full['contactId']; $subscription_data = json_encode(array(array( 'contactId' => $contactId, 'listId' => $listId, 'status' => 'normal' ))); $args = array( 'body' => $subscription_data, 'method' => 'POST', 'headers' => $headers, 'sslverify' => false, 'timeout' => 30 ); // assign a newly added contact to a list. $result_list = wp_remote_request($post_url.'subscriptions/', $args); } add_action( 'wpcf7_mail_sent', 'wpcf7_send_to_icontact', 1);
What this code does
In a nutshell, this code gets the input data from the contact form after email gets sent from the contact form, then first sends a HTTP post to iContact to add new contact to your account, and then sends a second HTTP post to put the new contact into a list.
The last line adds the action to Contact Form 7, so function “wpcf7_send_to_icontact” will run after a contact form is successfully sent.
add_action( 'wpcf7_mail_sent', 'wpcf7_send_to_icontact', 1);
The function first gathers relevant contact data. In this example, it’s pulling the first name, last name, and email address from the contact form data.
$formdata = $cfdata->posted_data; $contact_email = $formdata['your-email']; $contact_fname = $formdata['your-first-name']; $contact_lname = $formdata['your-last-name'];
To send a HTTP post, it uses a built-in WordPress function, wp_remote_request. So after gathering the contact data, it prepares the necessary array. There are two main parts:
- $contact_data is an array that matches the contact form data to the iContact contact fields. Note that field names are not the same as merge fields. You can reference this help doc: http://developer.icontact.com/documentation/contacts/
- $headers contains the header information for HTTP post, and it specifies json encoding and required information for authentication.
Then this sends the HTTP post to add the contact:
$result = wp_remote_request($post_url.'contacts/', $contact_args);
$result you get here contains the ID for the new contact which you need to do the second HTTP post. The actual ID is tucked away in an array several layers deep and the whole thing is json encoded, so this decodes the $result and gets the ID:
$result_body = json_decode(wp_remote_retrieve_body($result), true); $contact_result = $result_body['contacts']; $contact_full = $contact_result[0]; $contactId = $contact_full['contactId'];
Then finally, it prepares the second array and sends the second HTTP Post to add the contact to a list:
$result_list = wp_remote_request($post_url.'subscriptions/', $args);
Debug
I used the following code to debug the code. The good thing about this integration method is that you get a clear result from the HTTP post call. So copy the following and put it in at the end of the wpcf7_send_to_icontact function. This will create a text file, “devlog.txt” in your server root folder (you may need to create it manually) and prints key variable contents and results from the HTTP post. You can add whatever variable you want to check in the $debug array.
IMPORTANT: Make sure that you either comment out/erase this part of the code once you are done, and erase the devlog.txt file otherwise you will be posting sensitive contact information on your server.
$debug = array( 'time' => date('Y.m.d H:i:s e P'), 'contact_arg' => $contact_args, 'result' => $result, 'result_body' => $result_body, 'contact_result' => $contact_result, 'contact_full' => $contact_full, 'contactId' => $contactId, 'subscription_post' => $subscription_data, 'result_list' => $result_list ); $test_file = fopen('devlog.txt', 'a'); $test_result = fwrite($test_file, print_r($debug, TRUE));
More examples
Here is a variation of the code for having an opt-in check box. For this to work, create a checkbox with a name “icontact-opt-in” with a single option.
function wpcf7_send_to_icontact($cfdata) { // get the form content $formdata = $cfdata->posted_data; $contact_email = $formdata['your-email']; $contact_fname = $formdata['your-first-name']; $contact_lname = $formdata['your-last-name']; // run only if opt-in box is checked if( $formdata['icontact-opt-in'] ) { // set list ID for the forms $listId = '------List ID------'; //this is the base api post http url $post_url = '------API URL------'; $headers = array( 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Api-Version' => '2.0', 'Api-AppId' => '------Application ID------', // app id here 'Api-Username' => '------Account Userame------', // this is the log in username 'Api-Password' => '------Application Password', // this is the password for api app 'user-agent' => 'Contact Form 7 Integration' // this can be whatever you want ); $contact_data = json_encode(array(array( 'email' => $contact_email, 'firstName' => $contact_fname, 'lastName' => $contact_lname ))); $contact_args = array( 'body' => $contact_data, 'method' => 'POST', 'headers' => $headers, 'sslverify' => false, // Set SSL verify to false because of server issues. 'timeout' => 30 ); // http post to add contact to iContact $result = wp_remote_request($post_url.'contacts/', $contact_args); // get the contact ID from the result $result_body = json_decode(wp_remote_retrieve_body($result), true); $contact_result = $result_body['contacts']; $contact_full = $contact_result[0]; $contactId = $contact_full['contactId']; $subscription_data = json_encode(array(array( 'contactId' => $contactId, 'listId' => $listId, 'status' => 'normal' ))); $args = array( 'body' => $subscription_data, 'method' => 'POST', 'headers' => $headers, 'sslverify' => false, 'timeout' => 30 ); // assign a newly added contact to a list. $result_list = wp_remote_request($post_url.'subscriptions/', $args); } // end if } add_action( 'wpcf7_mail_sent', 'wpcf7_send_to_icontact', 1);
Here is another example for using form title to add contact to two different lists. It will not run unless the form title is a match.
function wpcf7_send_to_icontact($cfdata) { // get the form content $formdata = $cfdata->posted_data; $contact_email = $formdata['your-email']; $contact_fname = $formdata['your-first-name']; $contact_lname = $formdata['your-last-name']; //get the form title. $formtitle = $cfdata->title; // set list ID for the forms depending on the form used. only run if contact comes from these lists if ( $formtitle == 'Contact Form Number 1' || $formtitle == 'Contact Form Number 2' ) { if ( $formtitle == 'Contact Form Number 1' ) $listId = '------List ID for the first form------'; elseif ( $formtitle == 'Contact Form Number 2' ) $listId = '------List ID for the second form------'; //this is the base api post http url $post_url = '------API URL------'; $headers = array( 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Api-Version' => '2.0', 'Api-AppId' => '------Application ID------', // app id here 'Api-Username' => '------Account Userame------', // this is the log in username 'Api-Password' => '------Application Password', // this is the password for api app 'user-agent' => 'Contact Form 7 Integration' // this can be whatever you want ); $contact_data = json_encode(array(array( 'email' => $contact_email, 'firstName' => $contact_fname, 'lastName' => $contact_lname ))); $contact_args = array( 'body' => $contact_data, 'method' => 'POST', 'headers' => $headers, 'sslverify' => false, // Set SSL verify to false because of server issues. 'timeout' => 30 ); // http post to add contact to iContact $result = wp_remote_request($post_url.'contacts/', $contact_args); // get the contact ID from the result $result_body = json_decode(wp_remote_retrieve_body($result), true); $contact_result = $result_body['contacts']; $contact_full = $contact_result[0]; $contactId = $contact_full['contactId']; $subscription_data = json_encode(array(array( 'contactId' => $contactId, 'listId' => $listId, 'status' => 'normal' ))); $args = array( 'body' => $subscription_data, 'method' => 'POST', 'headers' => $headers, 'sslverify' => false, 'timeout' => 30 ); // assign a newly added contact to a list. $result_list = wp_remote_request($post_url.'subscriptions/', $args); } // end if } add_action( 'wpcf7_mail_sent', 'wpcf7_send_to_icontact', 1);