How to Integrate Salesforce Webs to Leads and WordPress Contact Form 7

A lot of my clients use Contact Form 7 on their wordress site to painlessly create their contact forms. And a lot of my clients also have a CRM of some sort setup running behind the scenes. One of my most common requests is to integrate my clients website with their CRM – in most cases this means taking web leads and plugging them straight into the CRM.

Alex Hager wrote an awesome article a while back explaining this process exactly. I would suggest you read that article first as it will help you get a better understanding of how this process work.

I implemented Alex’s code on my clients site (of course I updated for my field names) and found that nothing came across. I also read the comments on Alex’s site and some people where getting the same issue.

I noticed that in Alex’s code he includes a small validation IF statement:

[dt_code]

if (!empty($first_name) && !empty($last_name) && !empty($email) ) {

[/dt_code]

I removed this if statement because my form only captures one name (which we assume is the first name). Once I had removed this only 1 field mapped into Salesforce: a field which was set by default to a text value and not assigned dynamically by PHP!

So I could see that my code is ok going into Salesforce, it was capturing the variables that was wrong!

Contact Form 7 – posted_data changes from version 3.9

After some research I found that a contact form 7 method called “posted_data” no longer works for security reasons.

You can read about the security updates to CF7 here

To work around this you need setup a little workaround:

[dt_code]

$submission = WPCF7_Submission::get_instance();

$data = $submission->get_posted_data();

[/dt_code]

 

This then allows you to grab the values of your CF7 fields and store them as PHP variables. The method to do this is:

[dt_code]

$phpVar = isset($data[‘fieldName’]) ? $data[‘fieldName’] : “”;

[/dt_code]

In this case fieldName is the unique identifier for the field which you set in Contact Form 7, usually something like: your-name or your-email and phpVar is just a variable name – you can call it whatever your want i.e. $name or $email.

 

Ready to Roll!

Using these updated techniques you can grab CF7 field values, store them as variables and pass them to a server for processing. In this case we use cURL to connect to Salesforce and POST the values into SF but this technique could be used for a whole bunch of services like Zoho and Mailchimp.

 

6 thoughts on “How to Integrate Salesforce Webs to Leads and WordPress Contact Form 7”

  1. Can you please post a full example of code with these updates? I can’t seem to get it working. Form submits fine, but never reaches Salesforce. Using WP 4.3

  2. No worries here is the full code:

    //CF7 Hook
    add_action( ‘wpcf7_before_send_mail’, ‘my_conversion’ );
    function my_conversion( $cf7 )
    {
    /* Use WPCF7_Submission object’s get_posted_data() method to get it. */
    // Gets current SUBMISSION instance
    $submission = WPCF7_Submission::get_instance();

    // Get submission data
    $data = $submission->get_posted_data();

    //extract posted data

    $firstName = isset($data[‘fName’]) ? $data[‘fName’] : “”;
    $lastName = isset($data[‘lName’]) ? $data[‘lName’] : “”;
    $email = isset($data[‘Email’]) ? $data[‘Email’] : “”;
    $phone = isset($data[‘Phone’]) ? $data[‘Phone’] : “”;
    $message = isset($data[‘Message’]) ? $data[‘Message’] : “”;
    $suburb = isset($data[‘Suburb’]) ? $data[‘Suburb’] : “”;

    //convert extracted data to POST request for SF
    $post_items[] = ‘oid=00D900000010xmq’;
    $post_items[] = ‘first_name=’ . $firstName;
    $post_items[] = ‘last_name=’ . $lastName;
    $post_items[] = ’email=’ . $email;
    $post_items[] = ‘phone=’ . $phone;
    $post_items[] = ‘description=’ . $message;
    $post_items[] = ‘address=’ . $suburb;

    $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

    }

      1. Thanks Matija, I wonder if oid now needs to be updated to orgid across the board? Can you test using orgid instead of oid for leads? And I will update my code.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top