Archiv für Februar, 2011

How to integrate Salesforce in Contact Form 7

A few weeks ago, I had to find a way to integrate leads from my WordPress sites into Salesforce. On most of my sites I use the popular plugin Contact Form 7. I spent hours searching for a way how I can integrate all the incoming leads from Contact Form 7 or cForms 2 (which will be my next post.) with Salesforce.

I found a way how to create leads in Salesforce using PHP: Salesforce´s Web to Lead. After that, I only had to integrate it somehow into Contact Form 7. And that is what I did. Contact Form 7 offers a hook that enables you to run your own function after form submisson called “wpcf7_before_send_mail“. I used this hook to collect the form data and create a lead in Salesforce using cURL.

Salesforce

Before you can start, you have to activate Web2Lead in Salesforce and create a Web-to-Lead Form:

  • Go to Setup (go to you name on the top right and select Setup on the drop-down menu)
  • Select Customize -> Leads -> Web-to-Lead on the left sidebar.
  • Activate Web-to-Lead and create your Web-to-Lead form.

When you generate your code, you will find a hidden field called “oid”. Copy the value of this field an replace <YOU_SALESFORCE_OID> (you will find this in my code) with it. And you are done!

Contact Form 7

In your CF7 Form, make sure that each field has the right id. Take a look at $_POST data in my code to find the right IDs. Here is an example how your CF7 form code could look like:

1
2
3
4
5
<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 ]
<label>Message <span>*</span></label>[textarea* your-message ]

And here we go: Just add this code to your functions.php of your WordPress theme. I´m sure this code could be cleaner but it´s a start:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
add_action( 'wpcf7_before_send_mail', 'my_conversion' );
function my_conversion( $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"];
  $company = $cf7->posted_data["your-company"];
  $message  = $cf7->posted_data["your-message"];
  $lead_source = $cf7->title;
 
  $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;
  $post_items[] = 'company=' . $company;
  $post_items[] = 'description=' . $message;
  $post_items[] = 'lead_source=' . $lead_source;
 
  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
  }
}

14 Kommentare