In this tutorial, you will learn about how to create leads in Salesforce programmatically, without the use of plugins. This tutorial focuses on integrating Contact Form 7 to SF using the Web-to-Lead method.

The full code can be found at the end of the tutorial.

If you are interested in a different integration please let us know in the comments section.

First we need to hook into the CF7’s “before send email”.

add_action("wpcf7_before_send_mail", "web_to_salesforce");
Code language: JavaScript (javascript)

We then create a function where we do the logic. The CF7 hook passes one variable which I called “$cf7”.

function web_to_salesforce($cf7){ // do logic here }
Code language: PHP (php)

Inside our function we retrieve the submission instance and the current contact form information.

function web_to_salesforce($cf7){ $submission = WPCF7_Submission::get_instance(); $wpcf = WPCF7_ContactForm::get_current(); }
Code language: PHP (php)

We can use the $wpcf object to to get the id and compare it to the id of the CF7 form we want to target. Bonus: we can also use the object to get the mail prop.

Using the $submission object we get the user-posted form data.

function web_to_salesforce($cf7){ $submission = WPCF7_Submission::get_instance(); $wpcf = WPCF7_ContactForm::get_current(); if( $wpcf->id == 99){ // 99 being the id of the CF7 form if ( !$submission ) { return false; } $mail = $wpcf->prop('mail'); $posted_data = $submission->get_posted_data(); } }
Code language: PHP (php)

In the next step, we prepare the fields we want to send to Salesforce.

$url = 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8'; $fields = array( 'debug' => 0, // set to 1 to active debug mode 'debugEmail' => 'admin@website.com', 'oid' => '00A100000000abc', 'first_name' => $posted_data['first-name'], 'last_name' => $posted_data['last-name'], 'email' => $posted_data['email'], 'phone' => $posted_data['telephone'], 'company' => $posted_data['company'], 'state' => $posted_data['state'], 'description' => $posted_data['enquiry'], 'country' => $posted_data['your-country'], 'emailOptOut' => true, 'lead_source' => 'Contact form', );
Code language: PHP (php)

We then call our cURL function that we will create next.

curl_send_to_salesforce($url, $fields);
Code language: PHP (php)

Bonus: you can edit the email CF7 recipient here based on various rules on the posted data.

$mail['recipient'] = 'sales@website.com'; // Save the email to field $wpcf->set_properties(array( "mail" => $mail )); // Return current cf7 instance return $wpcf;
Code language: PHP (php)

Now for our cURL function:

function curl_send_to_salesforce($url, $fields){ // URL-ify the data for the POST foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string, '&'); // Open connection $ch = curl_init(); // Set the url // Set the number of POST vars // Set the POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); return $result; }
Code language: PHP (php)

Now for the full code:

<?php function web_to_salesforce($cf7){ $submission = WPCF7_Submission::get_instance(); $wpcf = WPCF7_ContactForm::get_current(); if( $wpcf->id == 99 ){ // 99 being the id of the CF7 form if ( !$submission ) { return false; } $mail = $wpcf->prop('mail'); $posted_data = $submission->get_posted_data(); $url = 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8'; $fields = array( 'debug' => 0, // set to 1 to active debug mode 'debugEmail' => 'admin@website.com', 'oid' => '00A100000000abc', 'first_name' => $posted_data['first-name'], 'last_name' => $posted_data['last-name'], 'email' => $posted_data['email'], 'phone' => $posted_data['telephone'], 'company' => $posted_data['company'], 'state' => $posted_data['state'], 'description' => $posted_data['enquiry'], 'country' => $posted_data['your-country'], 'emailOptOut' => true, 'lead_source' => 'Contact form', ); curl_send_to_salesforce($url, $fields); $mail['recipient'] = 'sales@website.com'; // Save the email to field $wpcf->set_properties(array( "mail" => $mail )); // return current cf7 instance return $wpcf; } } function curl_send_to_salesforce($url, $fields){ // URL-ify the data for the POST foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string, '&'); // Open connection $ch = curl_init(); // Set the url // Set the number of POST vars // Set the POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); return $result; }
Code language: HTML, XML (xml)

Hope this helped you get a head start with your integration. If you have any questions or need help with anything else, feel free to leave a comment below.

Published by Dragos Micu

WooCommerce

9 Comments

  1. Hello Dragos,
    first thank you for the slick script, the one I had before was quite longer and had more code… 🙂

    Your script used to work beautifully on my site, but since a few days all dropdown fields in CF7 are send as “=array” to salesforce.

    Do you have an idea what could have caused this? The last CF7 Update perhaps?

    Kind Regards

    Seb

    Reply
  2. Hi! This plugin works for me, but only in debug mode 🙁 If i turn off debug, dosent send the user to my salesforce account. Any ideas?

    Thanks in advance 🙂

    Reply
    • Hi Benjamin,

      Are you using Google ReCaptcha by any chance? Also, are you getting any errors in the Network’s XHR tab?

      Best wishes
      Dragos

      Reply
      • Hi! Its working 😉 The thing is that its takes one hour aprox to show data in Salesforce. It could be something with my salesforce or my WordPress? I am using the code in a sandbox area.

        Thanks again and sorry for so many questions 🙁

        Reply
        • Hi Benjamin,

          I believe it’s usual with Salesforce to sometimes take a while to show up the data. It happed to me before also.

          Best wishes,
          Dragos

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

campobet casino dk resena starcasinodice resena bingo australia casino resena betsoft birds guias descarga juegos de slot gratis para celular disfruta de la emoci ea1b7a72 n sin gastos play boom casino resena jammin jars 21 blackjack online espaol blog juega ruleta en l nea sin descargar la comodidad de la diversi n en tu ee00256 casa guias el casino b1f0 de galer as pachuca un lugar de diversi n y entretenimiento en la ciudad reportajes bonos de casino sin dep sito en europa revisi n 51f8431 y an lisis mx betsson descargar app apuestas revisi n y an lisis de ganancias en baccarat la experiencia en 21luckybet 6ebccb5 casino apuestas rese a 26ebcc completa de betking casino en per es confiable y pago guias 0ea1b7a recuerdos de la noche orquestas que marcaron un punto en el casino agua caliente tijuana apuestas tragaperras modernas gratis juega sin 26ebcc descargar en cualquier momento juego casino de 15 a os a902a8 en hermosillo un recordatorio del pasado bonos casino online sin deposito reportajes guardias de seguridad para casinos en guadalajara protecci n 1f843197 y confianza juego casinos en l nea en argentina la mejor gu a para jugar de forma segura 02a8016d guias el jefe de barra un l ea1b7a72 der de meseros en el casino mexico apuestas y 71bac87f pron sticos para sporting cp vs benfica el 31 de julio de 2026 qu opciones tienes en bc game wild 24 casino resena tarjetas de credito