Gravity Forms is an advanced form builder for Wordpress websites and can be placed into the Wordpress functions.php file. These forms can integrate with Lead Gen & CRM forms.
Article Contents
- Important Information Regarding Form Scripts
- Before You Connect
- Creating Third Party Forms
- Adding baseURI Embed Code to Gravity Forms
- Adding Endpoint Embed Code to Gravity Forms
- Adding the WordPress connection to Gravity Forms
- Adding .PHP Code to Gravity Forms
- Mapping Third Party Forms
- Special Considerations for Gravity Forms
Trial | ✓ | |
Essential | ✓ | |
Advanced | ✓ | |
Ultimate | ✓ |
Administrators | ✓ | |
Company Managers | ||
Marketing Managers | ||
Sales Managers | ||
Salespersons | ||
Jr. Salespersons |
Important Information Regarding Form Scripts
The Form script that Lead Gen & CRM provides should not be placed on your page. It is only used to complete the procedures in this document.
The provided script is only used for reference, and it should not be on the page when connecting Gravity Forms via this method. The modified Wordpress .PHP code replaces what Lead Gen & CRM's Form script would normally do. If both are present, your form will not function as intended.
Before You Connect
The procedures in this article assume that you have done the following:
- Installed Gravity Forms on Wordpress
- Added a Gravity Form to at least one Wordpress page
- Inserted site tracking code on each Wordpress page you want to track
Be aware that if you have existing Gravity Forms that were connected using Lead Gen & CRM's Gravity Forms Integration Tool, you will need to use the tool to generate code for new forms.
The Gravity Forms Integration Tool generates a block of code for each form you intend to add. If you were to use this tool for new forms, you would need to remove the .PHP code in the forms and add two hidden fields to each of your Gravity Forms.
Creating Third-Party Forms
Before you can connect Gravity Forms to Lead Gen & CRM, you need to create a third-party form. To create third-party forms, do the following:
- Click Forms in the left toolbar.
- Click Create Form.
Click to enlarge.
- Enter a name for the form.
- Click the Third Party Form radio button and choose Gravity Forms from the dropdown.
- Click the appropriate radio button to determine how contacts who fill out the form should be assigned.
-
Optionally, click the Allow a lead or contact record to be created even if an email address is not submitted checkbox.
Note: You can choose to allow a lead or contact record to be created even if an email address is not submitted. Checking this option will allow duplicate records to be created since the email address is the unique identifier in Lead Gen & CRM. -
Click Continue.
Click to enlarge.
- The created third-party form will open a page with an embed code. Keep this embed code window open. You will need the embed code later.
Click to enlarge.
Adding baseURI Embed Code to Gravity Forms
Once you have created a third-party form in Lead Gen & CRM, you can begin connecting Gravity Forms. The connection process requires that you add both baseURI
and endpoint
information in connected forms. This information comes from the embed code generated by the created third party form in Lead Gen & CRM. To add baseURI
information to Gravity Forms, do the following:
- In another browser tab, log in to Gravity Forms.
- Create or edit a Gravity Form.
- Create a hidden field.
- Note: For best results, make these hidden fields the first fields on your form.
- Click the hidden field.
- Click General.
- Change the Field Label value to the following:
sharpspring_base_uri
- Click Advanced.
- Return to the Lead Gen & CRM embed code browser tab.
- Copy the URL in the
baseURI
line in the embed code. -
Note: When copying the
baseURI
string, do not include the single quotation marks. - Return to the Gravity Forms browser tab.
- Paste the copied
baseURI
URL in the Default Value text field.
Example code used.
Click to enlarge.
Be aware that endpoint
information is still needed to complete the connection process.
Adding Endpoint Embed Code to Gravity Forms
Once you have added baseURI
information to Gravity Forms, you will need to add endpoint
information. This information comes from the embed code generated by the created third party form in Lead Gen & CRM. To add endpoint
information to Gravity Forms, do the following:
- Create another hidden field in the modified Gravity Form.
- Note: For best results, make these hidden fields the first fields on your form.
- Click the hidden field.
- Click General.
- Change the Field Label value to the following:
sharpspring_endpoint
- Click Advanced.
- Return to the Lead Gen & CRM embed code browser tab.
- Copy the alphanumeric string of numbers and dashes in the
endpoint
line in the embed code. -
Note: When copying the
endpoint
string, do not include the single quotation marks. - Return to the Gravity Forms browser tab.
- Paste the copied
endpoint
string in the Default Value text field. - Update the modified Gravity Form.
Example code used.
Click to enlarge.
Adding the WordPress connection to Gravity Forms
Once you have added baseURI
and endpoint
information to Gravity Forms, you will need to enable Wordpress to capture it, which requires some additional code.
The simplest way to do this is by uploading the plugin file found here. Note that this is offered as a way to add the code to WordPress, and the other steps are necessary, aside from copying the PHP code. If there is any issue with using this plugin on your setup, your developer will need to explore adding the PHP code below manually.
- Download the plugin zip file attached to this article at the bottom.
- In WordPress go to Plugins, and choose to upload a plugin
- Upload the zip file.
- View your Plugins in WordPress and enable this plugin
If this gives you issues, or you prefer to add the code in your own way, please see the steps below. By using either the plugin or a child theme, it will prevent the code from being removed if your theme updates its functions.php file.
Adding .PHP Code to Gravity Forms
Note that this is only required if not using the plugin as noted above. If using the plugin, please proceed to the Mapping Third Party Forms section below.
There are certain things to consider when adding .PHP code to Wordpress, however:
-
You need to add .PHP code only once. If you add this code for each form, it would cause any number of issues. In addition, the main advantages of adding code to Wordpress are that you need only add the code once without editing it and do not modify the .PHP code otherwise. As such, when adding additional forms, you will only need to make a new third party form in Lead Gen & CRM and add the two hidden fields to your Gravity form.
- You should add the .PHP code to a Wordpress child theme, if at all possible. Modifying code works best with a Wordpress child theme. Modifying a child theme allows you to modify Wordpress without breaking its functionality.
To modify Wordpress .PHP code, do the following:
- Open another new tab in your web browser.
- Log in to Wordpress in that browser tab.
- Click Editor, located under Appearance in the left menu.
- Click the functions.php file in the right menu.
- Copy-and-paste the following block of code at the bottom of the functions.php file:
Important: Changes to the functions.php file in Wordpress can result in your site crashing. Be sure you have FTP access in order to backup and revert to your old functions.php file if needed.
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ){
if ( rgar( $entry, 'status' ) === 'spam' ) {
return false;
}
$body = [];
function dupeCheck($fieldName, $bodyData) {
$cleanLabel = substr(preg_replace("/[^a-zA-Z0-9]+/", "", $fieldName), 0, 24);
for ($x = 0; $x <= 20; $x++) {
if(array_key_exists($cleanLabel, $bodyData)) {
$cleanLabel = $cleanLabel . $x;
} else { break; }
}
return $cleanLabel;
}
$formFields = $form['fields'];
foreach($formFields as $formField):
if($formField['label'] == 'sharpspring_base_uri') {
$base_uri = rgar( $entry, $formField['id']);
$sendToSharpSpring = true;
} elseif($formField['label'] == 'sharpspring_endpoint') {
$post_endpoint = rgar( $entry, $formField['id']);
} elseif($formField['type'] == 'multiselect') {
$fieldNumber = $formField['id'];
$fieldLabel = dupeCheck($formField['label'], $body);
$tempValue = rgar ( $entry, strval($fieldNumber) );
$trimmedValue = str_replace('[', '', $tempValue);
$trimmedValue = str_replace(']', '', $trimmedValue);
$trimmedValue = str_replace('"', '', $trimmedValue);
$body[preg_replace("/[^a-zA-Z0-9]+/", "", $fieldLabel)] = $trimmedValue;
} elseif($formField['inputs']) {
if($formField['type'] == 'checkbox') {
$fieldNumber = $formField['id'];
$fieldLabel = dupeCheck($formField['label'], $body);
$checkBoxField = GFFormsModel::get_field( $form, strval($fieldNumber) );
$tempValue = is_object( $checkBoxField ) ? $checkBoxField->get_value_export( $entry ) : '';
$trimmedValue = str_replace(', ', ',', $tempValue);
$body[preg_replace("/[^a-zA-Z0-9]+/", "", $fieldLabel)] = $trimmedValue;
} elseif($formField['type'] == 'time') {
$fieldNumber = $formField['id'];
$fieldLabel = dupeCheck($formField['label'], $body);
$body[preg_replace("/[^a-zA-Z0-9]+/", "", $fieldLabel)] = rgar( $entry, strval($fieldNumber) );
} elseif($formField['type'] == 'date') {
$fieldNumber = $formField['id'];
$fieldLabel = dupeCheck($formField['label'], $body);
$body[preg_replace("/[^a-zA-Z0-9]+/", "", $fieldLabel)] = rgar( $entry, strval($fieldNumber) );
} else {
foreach($formField['inputs'] as $subField):
$fieldLabel = dupeCheck($subField['label'], $body);
$fieldNumber = $subField['id'];
$body[preg_replace("/[^a-zA-Z0-9]+/", "", $fieldLabel)] = rgar( $entry, strval($fieldNumber) );
endforeach;
} } else {
$fieldNumber = $formField['id'];
$fieldLabel = dupeCheck($formField['label'], $body);
$body[preg_replace("/[^a-zA-Z0-9]+/", "", $fieldLabel)] = rgar( $entry, strval($fieldNumber) );
};
endforeach;
$body['form_source_url'] = $entry['source_url'];
$body['trackingid__sb'] = $_COOKIE['__ss_tk']; //DO NOT CHANGE THIS LINE... it collects the tracking cookie to establish tracking
$post_url = $base_uri . $post_endpoint;
if($sendToSharpSpring) {
$request = new WP_Http();
$response = $request->post( $post_url, array( 'body' => $body ) );
}
}
- Click Save.
- Return to the Gravity Forms browser tab.
- Update the form.
- Fill out the form.
- Submit the form once.
Mapping Third Party Forms
Once you have created a third party form, added third party form embed code, and modified Wordpress' functions.php file, you can map the fields in the third party form. Mapping form fields provides Lead Gen & CRM with information on what those form fields refer to. To map third party forms, do the following:
- Return to the Lead Gen & CRM embed code browser tab.
- Click Refresh this Page.
- Click a field drop-down menu in the Field Mappings page that appears.
- Select the option that most closely matches the field.
- Repeat as necessary for every form field.
- Click Save Mappings.
Whenever you fill out the form for the first time, you generate mapping data. As such, do not expect the lead to appear in the system until after the first entry. The second person to fill out the form will be imported as a lead.
Special Considerations for Gravity Forms
When mapping fields and connecting Gravity Forms, consider the following:
-
Certain fields will not be mappable. As with any non-Constant Contact form, certain fields, including the GDPR Consent and File Upload fields, will not be available for mapping.
-
You may see some fields that are not on your form. For example, if you are using Gravity Forms’ default Name field, your form may only show the First Name and Last Name fields. However, this field has options to include other fields, such as Suffix or Middle Name. Skip mapping those fields if they are not on your form. This applies to any of Gravity Forms’ fields that contain multiple fields within themselves. Lead Gen & CRM only collects what is mapped.
-
Your field names will appear the same as in Gravity Forms, but any non-alphanumeric characters will be removed. For example, if your Gravity Form field is What is your favorite color?, the mapping screen will show Whatisyourfavoritecolor. This only appears on the mapping screen. Once mapping is saved, this will not be seen elsewhere.
-
The form_source_url field is always included and contains the URL from which the form was filled as provided by Gravity Forms. If you do not want to record this, skip mapping it.
-
Changing third party form or Gravity Form information will require remapping. For example, if you change the label on a field of your Gravity Form after mapping, you will need to reset the mapping and remap the form using the new field name.
-
If multiple fields on your form have the same label, all subsequent copies after the first will have a number following the field name. For example, the second copy will have 0 following the field name, followed by 1 on the third, and so on. This can include hidden fields, and this ensures that your data comes through if there is a hidden field with the same name.
-
Field names will show only the first 24 characters when mapping. This helps to prevent issues with Gravity Forms fields that have lengthy names.
-
Fields on your form must have labels in order to be collected by Lead Gen & CRM. If you do not want fields to have visible labels, mark field labels as hidden within Gravity Forms.
- Plugins may prevent collection. Lead Gen & CRM may not be able to collect special fields added to Gravity Forms via additional plugins. Some plugins may prevent form collection entirely as well.