WordPress Contact Form 7 hook for Salesforce lead creation

Few days back I was asked to write a php method which send information to Salesorce from contact form7 form. Initially I thought of adding my custom function to the functions.php file of contact-form7 plugin. But I understood that its not a good idea to write custom methods to the core file of any plugin as it may get overwritten when the plugin is updated. To avoid this I have decided to write my own custom plugin hooks the contact form7 and do the job

Here I will explain you how to write a simple wordpress plugin.

Creating a WordPress plugin

Create a file with extension .php, for example ContactForm7-Salesforce-plugin.php. Add the following to the beginning of the file, which will tell what the plugin does and who is the owner..etc

/*
Plugin Name: ContactForm7- Saleforce-Plugin
Plugin URI: https://kunjans.wordpress.com
Description: Custom plugin which send info to create leads in Salesforce
Author: Aneesh Kunjan @kunjans.wordpress.com
Version: 1.0
Website: https://kunjans.wordpress.com
*/

We have to write our own custom php functions below the plugin info, and the plugin needs to be uploaded to the path /wp-content/plugins/

Understanding the CF7 object

Lets have a look at CF7 object. CF7 objects are arrays so you always need to call the CF7 object like object->object['array']. Also, returning the object at the end of the function is not required but a good practice.

Contact Form 7 action hook examples

So that’s pretty much it, creating a WordPress action hook to the wpcf7_before_send_mail and understanding how to traverse the object should allow any PHP developer to do pretty much anything with it. Let’s see some the below example which pushes contactform7 data to salesforce.

Lets do some coding

/* define your custom method and hook */

add_action( ‘wpcf7_before_send_mail’, ‘sendtosalesforce’ );

/*your custom method*/
function sendtosalesforce($cf7) {

$submission = WPCF7_Submission::get_instance();
if($submission) {
//featch post data
$posted_data = $submission->get_posted_data();
$email = $posted_data[“your-email”];
$company = $posted_data[“your-subject”];
$lastname = $posted_data[“your-name”];
$firstname = $posted_data[“your-firstName”];
$url = ‘https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8’;        $fields = array(
‘last_name’=>urlencode($lastname),
‘first_name’=>urlencode($firstname),
‘company’=>urlencode($company),
’email’=>urlencode($email),
‘oid’ => “”, //Org Id
//’retURL’ => urlencode(‘http://google.com/’), // sending this just in case
‘debugEmail’ => urlencode(“”),
‘lead_source’=>’Website’,        );

//url-ify the data for the POST

$fields_string = “”;

foreach($fields as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }        rtrim($fields_string,’&’);

if($posted_data[“your-phone”] && $posted_data[“text-form”])
{
$phone =$posted_data[“your-phone”];
$desc = $posted_data[“text-form”];
$fields_string .= ‘phone=’.urlencode($phone).’&’.’description=’.urlencode($desc);
}
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);        curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

}//post data ends

}

That’s it!

You should now have total control over the Contact Form 7 plugin. For any other examples or questions on the code please feel free to comment below, for other issues please visit the Contact Form 7 official WordPress forum.