Integrate WordPress Contact Form 7 and Salesforce Web to Lead

we need to write the curl (php) funictionality in functions.php file in wordpress server. login to your ftp server and you can find the file name called functions.php in your wp-includes folder with following path:

SFDC_WordPress blue-plus ContactForm7

httpdocs / wp-content / plugins / contact-form-7 / includes / functions.php

HTML CODE:

<label>First Name <span>*</span></label>[text* your-firstname ]
<label>Last Name <span>*</span></label>[text* your-lastname ]
<label>Email <span>*</span></label>[email* your-email ]
<label>Phone</label>[text your-phone ]

PHP CODE: httpdocs / wp-content / plugins / contact-form-7 / includes / functions.php

add_action( 'wpcf7_before_send_mail', 'leadFromWebtoSFDC' );
function leadFromWebtoSFDC( $cf7 ) {
$email = $cf7->posted_data["your-email"];
$first_name  = $cf7->posted_data["your-firstname"];
$last_name  = $cf7->posted_data["your-lastname"];
$phone = $cf7->posted_data["your-phone"];

$post_items[] = 'oid=<YOU_SALESFORCE_OID>';
$post_items[] = 'first_name=' . $first_name;
$post_items[] = 'last_name=' . $last_name;
$post_items[] = 'email=' . $email;
$post_items[] = 'phone=' . $phone;

if(!empty($first_name) && !empty($last_name) && !empty($email) ) {
$post_string = implode ('&', $post_items);
// Create a new cURL resource
$ch = curl_init();

if (curl_error($ch) != "") {
// error handling
}

$con_url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
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);
curl_exec($ch); // Post to Salesforce
curl_close($ch); // close cURL resource
}
}

Written by 

Salesforce MVP | Lightning Champion | 16x Salesforce Certified | Application Architect | 6x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*