How to Pass and Store a Salesforce Contact ID with Gravity Forms and WordPress

Salesforce Gravity Forms WordPress

My Salesforce Partner agency is working with an enterprise organization right now to implement Salesforce, Marketing Cloud, Mobile Cloud, and Ad Studio. Their websites are all built on WordPress with Gravity Forms, a fantastic form and data management tool that has a ton of capabilities. As they deploy campaigns via Marketing Cloud in email and Mobile Cloud in SMS, we’re configuring their account and processes to always pass the Salesforce Contact ID to any landing page with a form.

By passing contact data, we can populate each Gravity Forms submission with a hidden field to capture the Salesforce Contact ID so that the client can export the data and import the updated information into their CRM. Later iterations will include an automatic population of the data, but for right now we just want to ensure the data is saved appropriately.

There are a few scenarios that we want to incorporate into this strategy:

  • A user clicks a link in an email sent through an email campaign, SMS campaign, or customer journey. That URL has the Salesforce Contact ID automatically appended utilizing a querystring variable named contactkey. An example might be:

  • The destination page may not have a form on it, so we want to store the Salesforce Contact ID into a cookie so that it can be extracted later within a Gravity Form.
  • The destination page may have a Gravity Forms form on it, where we want to dynamically populate a hidden field that has the Salesforce Contact ID.

Storing a Salesforce Contact ID in a Cookie in WordPress

To capture and store the Salesforce Contact ID in a Cookie in WordPress, we’ll need to add code to our functions.php page in our active theme. We’re going to overwrite any Salesforce Contact ID that may already in an existing cookie as well, since many companies clean up records, remove duplicates, etc:

function set_SalesforceID_cookie() {
 if (isset($_GET['contactkey'])){
  $parameterSalesforceID = $_GET['contactkey'];
  setcookie('contactkey', $parameterSalesforceID, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);

Using this hook will set a cookie regardless of whether or not a form exists on the page. We also need to populate any Gravity Forms hidden field using the gform_field_value_{name} method and the cookie if there’s no Salesforce Contact ID is passed in the URL:

add_filter( 'gform_field_value_contactkey', 'populate_contactkey' );
function populate_utm_campaign( $value ) {
 if (!isset($_GET['contactkey'])){
   return $_COOKIE['contactkey'];

This is a first-party cookie, as well, which is advantageous to us.

Adding a Salesforce Contact ID Hidden Field in Gravity Forms

Within a Gravity Forms form, you’ll want to add a hidden field:

gravity forms add hidden field

Then, on your hidden field, you’ll want to set the Advanced option of setting your field to be populated dynamically with your querystring variable contactkey. If this sounds redundant… it is. In the event that a visitor blocks tracking via cookies, we can still populate the hidden field with the querystring variable:

gravity forms hidden field populate querystring

Gravity Forms has a ton of other prepopulation options that you can also programmatically incorporate on their site.

Implementation Upgrades

  • Remove Caching On Gravity Forms Pages – if Gravity Forms is on a cached page, you will not dynamically populate your field. This is a known issue and, thankfully, someone built a plugin that ensures any page with a Gravity Forms form is not cached, Fresh Forms for Gravity. Of course, one concern with this is if you’re loading a form on every page of your site… it will basically disable caching sitewide.
  • Gravity Forms Cookie Plugin – There’s an older plugin that’s not published on the WordPress repository but the code is available that you can add to your site and it stores any querystring variable to a cookie. I’ve not tested it, but it appears processing.
  • Gravity Forms Salesforce Add-On – I’m a bit disappointed that Gravity Forms doesn’t have an official Salesforce integration at this point, and it would be great to incorporate cookies into that implementation. I do wish I had the time to develop this! They do offer a Zapier add-on that can integrate with Salesforce, but I’ve not tested it.

With this configuration, we’re now storing the Salesforce Contact ID as a cookie and populating any Gravity Forms data with it. Even if the user leaves the site and returns in another session, the cookie is set and will prepopulate the Gravity Forms field.

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.