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.


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:

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

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 = '';
    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!

48 Responses to How to integrate Salesforce in Contact Form 7

  1. Dani sagt:

    You made my day! And saved a lot of hours, crawling the internet for the solution. Thank you!

  2. alex sagt:

    My pleasure, Dani!


  3. JHouse sagt:


    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. JHouse sagt:

    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. alex sagt:

    Glad to hear that, JHouse!

  6. Stephen sagt:

    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. alex sagt:

    Hey Stephen,

    Thanks for your comment. There is no need to add it to functions.php

  8. Chris Dawson sagt:

    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. Chris Dawson sagt:

    could you clarify where “wpcf7_before_send_mail” goes – in ‘additional settings’ just like that:

    does it need some other hook or call there?

  10. alex sagt:

    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. Vipin sagt:


    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,

    on_sent_ok: “location = ‘’;”

    Is it correct?

    please help

  12. Duncan sagt:

    I cannot get this to work at all. Alex, could you please just check my code to see if i have done anything wrong?

    I have 2 forms on my website. Can i write 2 functions to get both forms to work?

  13. Duncan sagt:

    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. Sepp sagt:

    *dank dalass*
    das hab ich gesucht ! Jo cool sache Parker ! ;) thx

  15. Andrew sagt:

    it worked great man, thanks a lot.

  16. Karen sagt:

    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. Robert sagt:

    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. Babar sagt:

    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

  19. Ian sagt:

    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. alex sagt:

    Can you create a var_export or print_r of the $cf->posted_data ?

  21. Rian sagt:

    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:

    - 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

    - create a field that looks something like this [text your-custom1]

    - 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. alex sagt:

    I´m glad I could help!

  23. Narinder sagt:

    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. Evie sagt:


    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.


  25. Evie sagt:

    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. Laxman sagt:

    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. Yemi sagt:

    Thanks so much Alex, this worked like a charm!

  28. Ed Reckers sagt:


    Thanks for your post. You can also use WordPress’s wp_remote_post function to clean things up a bit and possibly provide a bit better support for a wider arrange of servers/hosts:

    This is your same basic function except that it uses the core WordPress function wp_remote_post:

    add_action( ‘wpcf7_before_send_mail’, ‘my_conversion’ );
    function my_conversion( $cf7 ) {

    // post url
    $url = “″;

    // set the post data
    $payload = array(
    “oid” => “”,
    “Campaign_ID” => “”,
    “first_name” => $cf7->posted_data["your-firstname"],
    “last_name” => $cf7->posted_data["your-lastname"],
    “email” => $cf7->posted_data["your-email"],
    “phone” => $cf7->posted_data["your-phone"],
    “company” => $cf7->posted_data["your-company"],
    “lead_source” => $cf7->title,

    // set the arguments
    $args = array(
    ‘method’ => ‘POST’,
    ‘timeout’ => 45,
    ‘sslverify’ => true,
    ‘headers’ => array(‘Content-type: text/html; charset=UTF-8′),
    ‘body’ => $payload

    // make the request and store the response
    $response = wp_remote_post( $url, $args );

    // print failure to the error log
    if( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    error_log( ‘WP POST ERROR’ );
    } else {
    // we’re good


  29. Gagan sagt:

    What we do if i have 3 different contact 7forms.

  30. Jay sagt:

    JHouse :Yes, that’s what it was, I just needed to add “wpcf7_before_send_mail” in the Additional Settings’ part.Thanks again for solution!

    Was this all that was added to the additional settings?

  31. Brett Moore sagt:

    Hi Alex, thanks for your hard work. While popular and effective, Contact 7′s documentation is horrible…

    I have followed your instructions, but no success yet. I have but custom web2lead integrations in PHP in the past, so I am pretty sure my code is correct, but the additional settings don’t seem to actually cause anything to happen. It is the /themes/yourtheme/functions.php file it needs to be added to correct (not won of the countless other funtions.php in worpress?)

  32. Troy sagt:

    Very helpful Alex. Thank you.

  33. Ben Jones sagt:

    I’m trying to prefill a value, like using a hidden field. Can you check the line that references $recordtype and let me know what is wrong? It is not sending the value into Salesforce.

    $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"];
    $state = $cf7->posted_data["state-323"];
    $message = $cf7->posted_data["your-message"];
    $jobtype = $cf7->posted_data["service-796"];
    $recordtype = “012400000001B1m”;
    $lead_source = $cf7->title;

    $post_items[] = ‘oid=XXXX’;
    $post_items[] = ‘first_name=’ . $first_name;
    $post_items[] = ‘last_name=’ . $last_name;
    $post_items[] = ‘email=’ . $email;
    $post_items[] = ‘phone=’ . $phone;
    $post_items[] = ‘state=’ . $state;
    $post_items[] = ’00N40000002TVvT=’ . $message;
    $post_items[] = ’00N40000002TIeH=’ . $jobtype;
    $post_items[] = ‘recordType=’ . $recordtype;
    $post_items[] = ‘lead_source=’ . $lead_source;

  34. Paul sagt:

    Is it possible to tailor it to run on specific contact forms? i.e. one contact form sends to Salesforce but another form just emails me directly?

  35. alex sagt:

    Hey Paul!

    You could do something like

    if($cf7->title == "contact form 1")
    // run salesforce code

    I hope that helps.


  36. Derrick sagt:

    I’ve followed through the steps above but when I submit the form none of the data is passed through to SFDC. The form appears to submit as the conditional redirects work that I setup but the data just doesn’t get passed.

    Any suggestions?

  37. rajendra Singh sagt:

    Thank you very much Alex Hager

  38. I wanted to throw my 2 cents in, since I just spent a long time trying to figure this out. First off, there is no need to put anything in additional settings for this to work. As long as you put that code in the functions.php file, Contact Form 7 will find it and use it. I personally put that right at the top of the wp-content\plugins\contact-form-7\includes\functions.php file

    I was having trouble getting it to work. The form would submit, but the data wouldn’t go to Salesforce. After trying a million things, I finally decided to remove the if(!empty($first_name) && !empty($last_name) && !empty($email) ) conditional statement, and wouldn’t you know it, the form sent to Salesforce!

    Color me pink, I totally didn’t think about that if statement. In my form, i was using only the last_name field, because I use a Full Name field on my form, and if you only use one name field, last_name is the best bet to go with to send to SF. Because I was only using the last_name, that if statement was failing because of the !empty($first_name) variable. Very easy fix to remove that, it just wasn’t clicking in my head that that could have been the issue! I feel dumb, but maybe that’s the issue for others.

    My suggestion, remove the if statement and just run the code, see if it works. If it does, you know something is wrong in that!

    One other issue you may have, is in his code for the function, he has $cf7 as the variable. Change all instances of those to $wpcf7 , as that is the correct variable, for the most updated CF7 plugin.

  39. Kurt sagt:

    Having an issue with checkboxes. Your script is working great for all my other fields but when I use the checkbox it return the value ‘array’ in Salesforce. The email I get from Contact Form 7 is the correct value but for whatever reason Salesforce is giving it the value of ‘array’.

    Any ideas?


  40. Steve sagt:

    Any tips on how to map checkbox values from CF7 to Salesforce?

  41. yogesh sagt:

    Excellent! you saved a lots of our ours, thanks a lot for this valuable information.

  42. Ben sagt:

    Hi Alex, and thanks a lot for your great tutorial.

    I’ve just got one concern now. I have 5 different forms on my website and I would like to send the leads in 5 different campaigns according to the situation.
    I tried to add a if condition like you’ve suggested before

    if($cf7->title == “the_name_of_my_form”)
    // run salesforce code

    but the function is not applied and the lead isn’t created in SF. Is there any chance that the name of my form isn’t recognized?

    I also tried to add the function in the template of each page containing a form (with a different Campaign_ID each time) instead of the functions.php document but here, nothing works!

    I admit not being very gifted for php :) Some help would be very appreciated here.

    Thank you very much.

  43. Samantha sagt:

    For $lead_source, I’d like it to populate with the URL of the page they’re on. How would I do that?

  44. James Dillon sagt:

    I believe CF7 have recently updated their plugin and since this the following code and instructions no longer work for me.

    I wonder if anyone knows what changes have resulted in this or if anyone else is experiencing the same problem since the update?

  45. Kurt sagt:

    Your script will know longer work with the updated version of CF7. I just went through the exercise of updating my script based on your build.

    Read this.

    This is what I did to correct it but would sure love some feedback to know if there is a better way.

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );
    function my_conversion( $cf7 )
    $submission = WPCF7_Submission::get_instance();

    if ( $submission ) {

    $email = $submission->get_posted_data("Email_Address");
    $title = $submission->get_posted_data("Title");
    $first_name = $submission->get_posted_data("First_Name");
    $last_name = $submission->get_posted_data("Last_Name");
    $phone = $submission->get_posted_data("Telephone_Number");
    $facility = $submission->get_posted_data("Facility");
    $country= $submission->get_posted_data("Country");
    $state= $submission->get_posted_data("State");
    $comments = $submission->get_posted_data("Interest");
    $lead_source = $submission->get_posted_data("HowDidYouFindUs");
    $newsletter = $submission->get_posted_data("Newsletter");

    $post_items[] = 'oid=00';
    $post_items[] = 'title=' . $title;
    $post_items[] = 'first_name=' . $first_name;
    $post_items[] = 'last_name=' . $last_name;
    $post_items[] = 'email=' . $email;
    $post_items[] = 'phone=' . $phone;
    $post_items[] = 'company=' . $facility;
    $post_items[] = 'country=' . $country;
    $post_items[] = 'state=' . $state;
    $post_items[] = 'hH=' . $comments;
    $post_items[] = 'lead_source=' . $lead_source;
    $post_items[] = '9t=' . $newsletter;

    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 = '';
    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

    In the process of researching this I was asked “why don’t you use the wp_remote_post() function instead of CURL?” I have know answer but I thought someone else might help me understand this.

    Thank You!

  46. I just wanted to throw a note in here. I was having trouble getting the “Description” field to send to my Salesforce account. The issue turned out to be that I was using “Description=” instead of “description=” (note the lower case D). You’d think Salesforce would have a thing in their script to convert all field variable coming in to lower case….but I guess not. If anyone has an issue with any of their fields not getting to SF, check the case.

  47. I wanted to make one more note here. I have a couple fields being sent to Salesforce that may include an Ampersand (&). Because of how this script sends data with the “post_string = implode (‘&’, $post_items);” line, when a field has an Ampersand, everything after that ampersand would be gone. To remedy this, when you first get the string, add a str_replace to it, to change ampersands to “and”. Like this: $company = str_replace(‘&’, ‘ and ‘, $wpcf7->posted_data["company-name"]);

  48. Sal sagt:

    Thanks for the post, Alex.

    Kurt – Your solution worked for me with Contact Form 7 Version 4.0.1

    I’m trying to pass the IP Address using _remote_ip but that’s the only thing I can’t seem to get to work. Any thoughts?


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>