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






#1 von Dani am 9. März 2011 - 22:10
You made my day! And saved a lot of hours, crawling the internet for the solution. Thank you!
#2 von alex am 10. März 2011 - 01:48
My pleasure, Dani!
Cheers,
Alex
#3 von JHouse am 14. März 2011 - 19:37
Hi,
I’ve tried using this, but it hasn’t worked yet. Chances are though, I’m doing something wrong as it’s working for you and Dani.
I’ve implemented the code exactly as you have it, but I of course added the OID. From what I understand, it’s set up properly in the SalesForce admin area for my client as it works for their other site. Both sites, however, are using the same OID, so maybe that’s the problem.
Maybe supposed to add the hook “wpcf7_before_send_mail” in Additional Settings? I’m going to try that now.
Thanks for the tutorial and code thus far; I’m sure this will work soon.
#4 von JHouse am 14. März 2011 - 21:12
Yes, that’s what it was, I just needed to add “wpcf7_before_send_mail” in the Additional Settings’ part.
Thanks again for solution!
#5 von alex am 14. März 2011 - 21:15
Glad to hear that, JHouse!
#6 von Stephen am 22. Mai 2011 - 18:09
I’m excited to try this out – when I add “wpcf7_before_send_mail” to the Additional Settings do I need to paste the entire function that I am also putting in functions.php?
#7 von alex am 22. Mai 2011 - 21:00
Hey Stephen,
Thanks for your comment. There is no need to add it to functions.php
#8 von Chris Dawson am 10. Juni 2011 - 13:26
Many thanks for this. Where/how would i add the salesforce “Campaign_ID” value to this? Client has given me a Campaign_ID to use, not sure where to include this.
#9 von Chris Dawson am 10. Juni 2011 - 15:19
could you clarify where “wpcf7_before_send_mail” goes – in ‘additional settings’ just like that:
wpcf7_before_send_mail
does it need some other hook or call there?
#10 von alex am 10. Juni 2011 - 19:47
Hey Chris!
I don´t have a Campaign_ID in Salesforce. But try this: Go to Web-To-Leads in Salesforce. Click on “Create Web-to-Lead Form”, add the Camoaign_ID to the fields and generate the code.
You´ll find the answer how you have to name the field in there.
About the Additional Settings:
I haven´t done a thing in there and it works for me. Just try it and if it doesn´t work, add the hook excatly as you said.
#11 von Vipin am 15. September 2011 - 07:45
Hello,
I have tried the same steps, but it is not working for me
In the additional setting section, I have redirect page entry. And I have added “wpcf7_before_send_mail” like this,
wpcf7_before_send_mail
on_sent_ok: “location = ‘http://www.website.com/thank-you/';”
Is it correct?
please help
Vipin
#12 von Duncan am 3. November 2011 - 13:10
I cannot get this to work at all. Alex, could you please just check my code to see if i have done anything wrong? http://pastebin.com/Qbqe5jVQ
I have 2 forms on my website. Can i write 2 functions to get both forms to work?
#13 von Duncan am 3. November 2011 - 13:32
I am putting this whole block in my functions.php file. SHould i be putting anything in this “Additional Settings” box. There is very limited documentation on what this box does.
#14 von Sepp am 9. Dezember 2011 - 11:45
*dank dalass*
thx
das hab ich gesucht ! Jo cool sache Parker !