Lead Gen & CRM
How can we help you?
Search our help articles, video tutorials, and quickstart guides

You've got this. You've got us. Search our Knowledge Base to quickly find answers to your questions.

Integrating Lead Gen & CRM with Contact Form 7 Using .PHP

Article: 000050532
Updated: July 26, 2024

Connect the Contact Form 7 Wordpress plugin to Lead Gen & CRM

Contact Form 7 is one of the most popular form plugins for WordPress. Lead Gen & CRM can only integrate with Contact Form 7 4.8 or greater.

If you are using a previous version, upgrade to the latest version of Contact Form 7 prior to integrating with Lead Gen & CRM.

This article details how to integrate a Contact Form 7 form into Lead Gen & CRM with .PHP.


Article Contents


Users:
Administrators 
Company Managers 
Marketing Managers 
Sales Managers  
Salespersons  
Jr. Salespersons  


Using This Information

Note: Before following the procedures in this article, try instead integrating Contact Form 7 by way of JavaScript. Use this method of integration if that method does not work for you.

This article will detail a method to connect Contact Form 7 forms using .PHP code. When using this method, the Native Form code should not be on the page with the form, as it will all be done from the .PHP code.

If Native Form embed code was previously placed for the form, it must be removed. 

The .PHP detailed here can be added to your functions.php file in Wordpress. Some themes may ask you to place your custom code elsewhere in another .PHP file.

While you can place it there, be sure to test it directly from functions.php if there are any issues, as compatibility in other files may vary based on your Wordpress theme or other plugins.

Note: Constant Contact offers Professional Services to assist with custom coding.


Before You Begin Integrating

Some Wordpress themes may hide access to the functions.php file. The functions.php file can also be accessed using FTP. Contact your website administrator if you do not see the functions.php file.

Additionally, FTP access is recommended in case the functions.php file needs to be reverted, as incorrect code here can make the site inaccessible except from FTP.  Pay close attention to the code that you add to functions.php. If you add incorrect code, you may be locked out of your Wordpress account.

Create a backup of your current functions.php before making these changes and be prepared to replace it via FTP if needed.
 



Obtaining Native Form Information

Before you can begin integrating Contact Form 7 with Lead Gen & CRM, you will need to obtain certain form information.

To obtain the necessary form information, create or edit a native form.

Review the generated JavaScript embed code in the window that appears.

Native Form Embed Code
 

Keep this browser tab open.

Important: Do not close either the embed code modal window or the browser tab. You will need this information.


Example of Native Form Embed Code

Once the Native Form is created, it will display a block of code. The code is different for every created Native Form. The displayed code is similar to the following example:
 

<script type="text/javascript">
var __ss_noform = __ss_noform || [];
__ss_noform.push(['baseURI', 'https://app-XXXXXXXXXX.marketingautomation.services/webforms/receivePostback/MzYwN7EwMzcyAgA/']);
__ss_noform.push(['endpoint', '30e3bbfe-a5e7-4207-9237-e9f9e2ff80e9'])
</script>
<script type="text/javascript" src="https://koi-XXXXXXXXXX.marketingautomation.services/client/noform.js?ver=1.24" ></script>

 



Preparing the .PHP File

Before doing anything specific to your form, you will need to make modifications to your functions.php file in Wordpress.

To modify the functions.php file, do the following: 

  1. Open another new tab in your web browser.
  2. Log into Wordpress in that browser tab.
  3. Click Editor, located under Appearance in the left menu.
  4. Open the functions.php file in the right menu.
  5. Add the following code the bottom of the file:
     
    function submitToSharpSpring($data, $baseURI, $endPoint) {
      $params = '';
      foreach($data as $key => $value) {
        if(!is_array($value)) {
          $params = $params . $key . '=' . urlencode($value) . "&";
      }
        else {
        $params = $params . $key . '=' . urlencode(implode(',',$value)) . "&";    
      }
      }
    
      if (isset($_COOKIE['__ss_tk'])) {
        $trackingid__sb = $_COOKIE['__ss_tk'];
        $params = $params . "trackingid__sb=" . urlencode($trackingid__sb);
      }
      // Prepare URL
      $ssRequest = $baseURI . $endPoint . "/jsonp/?" . $params;
      // Send request
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $ssRequest); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      ob_start();
      var_dump($data);
      $data_dump = ob_get_clean();
      curl_close($ch);
    }
  6. Place the following code directly below the above code block:
     
    add_action( 'wpcf7_before_send_mail', function ($cf7) {
    // Your unique code for each form goes here
    });
  7. Paste the following code below the line that contains Your unique code for each form goes here and above the closing }); line:
     

    if ($_POST['_wpcf7'] == Your Form ID){
     $baseURI = 'your baseURI';
     $endpoint = 'your endpoint';
     submitToSharpSpring($_POST, $baseURI, $endpoint);
     }
     // If connecting another form, place its code below this one


Adding Additional Forms

To add additional forms, repeat Step 6 above. When doing so, add each code block below the end of the above code before it. The code should be added just under the comment that says: 
 

// If connecting another form, place its code below this one

Be aware that your baseURI will typically be the same for all forms within an instance of Lead Gen & CRM.
 



Adding Unique Code to Forms

Once the functions.php file has been modified, you can begin adding unique code to individual forms.

This only needs to be done once for each unique Contact Form 7 form, regardless of how many pages it exists on.


To add unique code to forms, do the following:

  1. Return to the Lead Gen & CRM browser tab with the Native Form embed code.
  2. Locate the line containing a web address that follows baseURI.
    In the above example, the web address is:
    https://app-XXXXXXXXXX.marketingautomation.services/webforms/receivePostback/MzYwN7EwMzcyAgA/
  3. Copy the web address, including the final forward slash.
  4. Return to the Wordpress functions.php browser tab.
  5. Replace your baseURI with the copied baseURI web address. It should remain in single quotes.
  6. Return to the Lead Gen & CRM browser tab with the Native Form embed code.
  7. Locate the line containing an alphanumeric value that follows endpoint.
    In the above example, the alphanumeric value is:
    30e3bbfe-a5e7-4207-9237-e9f9e2ff80e9
  8. Copy the value.
  9. Return to the Wordpress functions.php browser tab.
  10. Replace your endpoint value with the copied endpoint value. It should remain in single quotes.
  11. Return to the Wordpress functions.php browser tab.
  12. Locate the Form ID value within the Contact Form 7 Short Code.
    In the following example, the Form ID value is 533
    [contact-form-7 id="533" title="php cf7 test"]
  13. Replace Your Form ID with the Form ID number within the Contact Form 7 Short Code in the if ($_POST line. It should not be in quotes.
  14. Save the Wordpress functions.php file.
  15. Add the Contact Form 7 short code to your Wordpress page.


Example of Completed Code 

Below is an example of completed code that connects two Contact Form 7 forms. The first part of the code is based on the example tracking code shown above. The second part of the code would connect a form with a Form ID of 789 in Contact Form 7 while using values from a different code. 
 

function submitToSharpSpring($data, $baseURI, $endPoint, $track = true) {
  unset($data['_wpcf7']);
  unset($data['_wpcf7_version']);
  unset($data['_wpcf7_locale']);
  unset($data['_wpcf7_unit_tag']);
  unset($data['_wpcf7_container_post']);

   $params = '';
  foreach($data as $key => $value) {
    if(!is_array($value)) {
      $params = $params . $key . '=' . urlencode($value) . "&";
  }
    else {
    $params = $params . $key . '=' . urlencode(implode(',',$value)) . "&";    
  }
  }

  if (isset($_COOKIE['__ss_tk'])) {
    $trackingid__sb = $_COOKIE['__ss_tk'];
    $params = $params . "trackingid__sb=" . urlencode($trackingid__sb);
  }
  $sharpReq = $baseURI . $endPoint . "/jsonp/?" . $params;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $sharpReq); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  ob_start();
  var_dump($data);
  $data_dump = ob_get_clean();
  curl_close($ch);
}


add_action( 'wpcf7_before_send_mail', function ($cf7) {
 $baseURI = 'https://app-XXXXXXXXXX.marketingautomation.services/webforms/receivePostback/MzYwN7EwMzcyAgA/';
  if ($_POST['_wpcf7'] == 533) {
            $baseURI = 'your base URI';
            $endpoint = '30e3bbfe-a5e7-4207-9237-e9f9e2ff80e9';
  submitToSharpSpring($_POST, $baseURI, $endpoint);
 }
 // If connecting another form, place its code below this one

if ($_POST['_wpcf7'] == 789) {
            $baseURI = 'https://app-XXXXXXXXXX.marketingautomation.services/webforms/receivePostback/MzYwN7EwMzcyAgA/';
            $endpoint = '1234567-a5e7-bfe3-abcd-a956ae605b';
  submitToSharpSpring($_POST, $baseURI, $endpoint);
 }
  // If connecting another form, place its code below this one

});
 


Did this article answer your question?


Constant Contact Logo

Copyright © 2025 · All Rights Reserved · Constant Contact · Privacy Center