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

10 Kommentare

New York City – Wochenende

Vor kurzem waren wir ein Wochenende in der Stadt die niemals schläft, New York City. Unsere Unterkunft war direkt in Manhatten und mit der Subway erreichbar. Durch die Bustour haben wir viele Sights der Stadt gesehen und uns durch den Wind auch ordentlich verkühlt :) Auch hier wieder einige Bilder:

New York City

, , ,

1 Kommentar

Atlantic City – Wochenende

Unser Wochenende in Atlantic City – oder aus das Vegas der Ostküste – ist zwar schon eine Weile her, aber trotzdem einen Blogpost wert.

Hier einige Bilder:

Atlantic City

,

Keine Kommentare

Philadelphia, PA, USA – Der erste Bericht

Wow, wie die Zeit vergeht. Genau 9 Wochen bin ich nun schon hier in Amerika, genauer gesagt in Philadelphia. Die ersten Wochen waren ein ziemliches Chaos. Es dauert eben, bis man alles geregelt hat und wieder einen normalen Altag hat.Darum haben ich mir bis jetzt auch sehr wenig Zeit für den Blog genommen.

Um die erste Frage gleich mal zu beantworten: JA, es ist GEIL hier.

Ich habe mich ziemlich gut eingelebt, viele neue Leute kennengelernt, die Wohnung eingerichtet und schon etwas umhergereist. Bis jetzt haben wir Philadelphia, New York und Atlantic City erkundet. Das waren wirklich sind Städte und Erlebnisse, die wir da gesammelt haben. Hier sind einige Fotos von unseren Ausflügen:

Philadelphia

Das war aber noch lange nicht alles. Da wir auch noch eine Weile – haha 10 Monate – hier sind haben wir aber auch keinen Stress. Die nächsten Ausfügen werden uns zu den Niagara Fällen, nach Washington D.C., später dann nach Miami und Orlando führen. Somit haben wir schon einen Großteil unserer Reiseziele hinter uns.

Wenns Fragen gibt, hier darunter ist ein Kommentar-Feld :)

Schönen Grüße aus Philadelphia!

,

Keine Kommentare

Gewinnspiel: SYNCING.NET – Die Gewinner stehen fest

Gestern lief das Gewinnspiel von 2 x 2 Lizenzen für das Programm SYNCING.NET Professional auf meinem Blog alexhager.at aus. Mitgemacht haben 9 Teilnehmer, bei denen ich mich hiermit herzlichst bedanken möchte. Vielen Dank!

Aus diesen Teilnehmerpool wurde 2 Personen gezogen, die je 2 Lizenzen für Syncing.NET Professional erhalten:

Herzliche Gratulation!

Die Gewinner werden natürlich per E-Mail verständigt.

Nochmals DANKE an alle Teilnehmer!

Alex

, ,

2 Kommentare