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
  }
}

Weitere ähnliche Artikel:

flattr this!

54 Responses to How to integrate Salesforce in Contact Form 7

  1. Vivin sagt:

    Hi , If i have a multi select picklist ( a custom field in lead object )and if the user selects more than one value how to pass that value. I am new to php , so someone please give me the syntax/code.
    thanks
    vivin

  2. Deya G sagt:

    I did this and now I can’t see my webstie!!! I don’t know what to do !!! help!

  3. Dario sagt:

    Any idea why I get all empty?

    Form:

    First Name *[text* firstname ]
    Last Name *[text* lastname ]
    Email *[email* email ]
    Phone[text phone ]
    Message *[textarea* description ]
    Subject[text subject ]

    [submit class:btn class:btn-primary "Enviar Mensaje"]

    Functions.php

    add_action( ‘wpcf7_before_send_mail’, ‘my_conversion’ );

    function my_conversion( $cf7 )
    {
    $email = $cf7->posted_data["email"];
    $first_name = $cf7->posted_data["firstname"];
    $last_name = $cf7->posted_data["lastname"];
    $phone = $cf7->posted_data["phone"];
    $description = $cf7->posted_data["description"];
    $subject = $cf7->posted_data["subject"];

    $post_items[] = ‘oid=00Do0000000Kvgv’;
    $post_items[] = ‘first_name=’ . $first_name;
    $post_items[] = ‘last_name=’ . $last_name;
    $post_items[] = ‘email=’ . $email;
    $post_items[] = ‘phone=’ . $phone;
    $post_items[] = ‘description=’ . $description;
    $post_items[] = ’00No00000093eG7=’. $subject;

    //if(!empty($first_name) && !empty($last_name) && !empty($email) )
    if (true)
    {
    $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
    }
    }

  4. zaus sagt:

    Guys, you’re all working too hard. I did the heavy lifting for you ages ago — here’s a plugin, all you need to do is map the fields:
    https://wordpress.org/plugins/forms-3rdparty-integration/

    It works for CF7 (including the 3.9 update), Gravity Forms, and Ninja Forms.

    Now I’m really sorry I didn’t drop a comment back when I wrote the original plugin…

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>