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
Trial | ✓ | |
Essential | ✓ | |
Advanced | ✓ | |
Ultimate | ✓ |
Administrators | ✓ | |
Company Managers | ✓ | |
Marketing Managers | ✓ | |
Sales Managers | ||
Salespersons | ||
Jr. Salespersons |
Using This Information
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: 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.
|
|
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. 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:
- Open another new tab in your web browser.
- Log into Wordpress in that browser tab.
- Click Editor, located under Appearance in the left menu.
- Open the functions.php file in the right menu.
- 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); }
- 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
}); -
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:
- Return to the Lead Gen & CRM browser tab with the Native Form embed code.
- 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/
- Copy the web address, including the final forward slash.
- Return to the Wordpress functions.php browser tab.
- Replace
your baseURI
with the copiedbaseURI
web address. It should remain in single quotes. - Return to the Lead Gen & CRM browser tab with the Native Form embed code.
- Locate the line containing an alphanumeric value that follows
endpoint
.
In the above example, the alphanumeric value is:30e3bbfe-a5e7-4207-9237-e9f9e2ff80e9
- Copy the value.
- Return to the Wordpress functions.php browser tab.
- Replace your
endpoint
value with the copiedendpoint
value. It should remain in single quotes. - Return to the Wordpress functions.php browser tab.
- Locate the
Form ID
value within the Contact Form 7 Short Code.
In the following example, theForm ID
value is533
:[contact-form-7 id="533" title="php cf7 test"]
- Replace
Your Form ID
with theForm ID
number within the Contact Form 7 Short Code in theif ($_POST
line. It should not be in quotes. - Save the Wordpress functions.php file.
- 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
});