Contact Form 7 & Salesforce Integrating Salesforce with Contact Form 7 for WordPress

Contact Form 7 for WordPress doesn’t have an easy way to integrate with Salesforce, which is why many people use the WordPress to Lead for Salesforce plugin that is designed to connect WordPress forms to Salesforce. But, for those of you that must use Contact Form 7 to populate Salesforce Leads, read on.

One of the challenges with Contact Form 7 is that it removes the first two letters of default Salesforce ID from custom fields (it changes 00N… to N…).

If you are just using standard fields, you can use the instructions posted by Alex Hager, who I’d like to thank for his contribution. What follows is based on his post and other commenters.

Example Contact Form 7 Code

You’ll notice I have a custom field in this form, a text field where the visitor can enter the best time to be called. Contact Form 7 takes some tweaking to get custom fields to work.

This code is just an example – yours will look different.

<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>Best Time to Call?</label>[text* best-time-to-call ]

Code for functions.php

This code should be added to your functions.php file.

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"];
  $best_time = $cf7->posted_data["best-time-to-call"]; 

  $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[] = ’00NU00000031VX4=’.$best_time;

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

Stuck? We strongly recommend the WordPress to Lead for Salesforce plugin – it works smoothly, and integrates with Daddy Analytics in minutes.