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 !
#15 von Andrew am 11. März 2012 - 14:17
it worked great man, thanks a lot.
#16 von Karen am 5. April 2012 - 18:47
Hi Alex: Trying to create a WordPress form that loads data into a Salesforce custom object, a special type of contact. Any chance? Thanks!
#17 von Robert am 13. April 2012 - 21:41
Awesome tips, thanks!
- Have any suggestions on how to include custom fields in the form? Salesforce gives custom fields a name starting with 00, for example “00N00000394″, which is impossible to work with, and Contact Form 7 removes the first two numbers, making the field unusable. Argh!
#18 von Babar am 1. August 2012 - 08:27
How can i track the lead source? Suppose if someone comes on my website from PPC or Facebook Adv or anyother website? How can i track it.
I would much appreciate if someone can help me with this..
email me back
babar_waheed82@yahoo.com
#19 von Ian am 9. Oktober 2012 - 18:33
Works great, but can someone confirm how this works with a drop-down menu where I have three choices?
The following does not populate the SF field for me:
——
$reason = $cf7->posted_data["reason-for-enquiry"];
$post_items[] = ‘Reason_for_Enquiry__c=’ . $reason;
—–
The reason for your enquiry:*[select* reason-for-enquiry "I have a question" "I would like a demo" "I would like to become a reseller"]
—-
Any ideas?
Thanks, Ian.
#20 von alex am 9. Oktober 2012 - 18:45
Can you create a var_export or print_r of the $cf->posted_data ?
#21 von Rian am 6. November 2012 - 05:24
Thanks Alex, this worked and saved a lot of headaches!
I also successfully added custom salesforce lead fields. In case anyone else wants to do this, here’s how I did it:
IN SALESFORCE
- create the custom field (I’ll call it “custom1″)
- generate a web-to-lead form
- look at the code, find the custom1 input field, and grab the ID. it will be a bunch of numbers and letters, like this: 00NU00000031VX4
IN YOUR CONTACT FORM
- create a field that looks something like this [text your-custom1]
IN ALEX’S CODE
- below line 10 insert a line that looks something like this: $custom1 = cf7->posted_data["your-custom1"];
- below line 19 insert a line that looks like this: $posted_items[] = ’00NU00000031VX4=’.$custom1;
That should do the trick (at least it did for me).
#22 von alex am 21. November 2012 - 15:04
I´m glad I could help!
#23 von Narinder am 22. Januar 2013 - 01:48
i have tried to implement the code, everthing is working fine in code but the while we send data through CURL it’s not posting to Salesforce,even CURL is working on my server.
Any idea why this is happening ?
#24 von Evie am 25. Februar 2013 - 08:53
Hi,
I’ve been asked to integrate contact form 7 with salesforce by my company. I’ve followed each step suggested above, the email was successfully sent to my email, but it never created a contact record in Salesforce. Anyone can help me?
Thank you in advance.
Ev
#25 von Evie am 26. Februar 2013 - 02:22
Sorry. My bad. I didn’t know lead and contact is a totally different thing. Everything is sorted now. Your plugin ROCKS! Thank you so much.
#26 von Laxman am 26. Februar 2013 - 23:44
do you have a php or JavaScript code that sends an email and send to sales force same time.I have been spending so much time. I appreciate your help my friend. Thanks in advance.
#27 von Yemi am 19. März 2013 - 17:31
Thanks so much Alex, this worked like a charm!