Forms are Unable to Return Cookies into a Field

Hi there,

I have created a site-specific plugin, which simply stores a UTM code as a cookie, and then returns that cookie into a field value. But for some reason, when I submit the form, the field always comes back empty. The code is below.

Note #1: I have confirmed that the cookie is being set correctly by echoing it out on a test page.
Note #2: If I change the second function to simply return a string, such as "test", it populates the field correctly
Note #3: I have also attempted using gform_field_value_$parameter_name to accomplish this, but had the same issue. And I have attempted using functions.php instead of a plugin.

Here is my code:

//Save the utm_source parameter as a session cookie
function namespace_set_source() {
  if (isset($_GET['utm_source'])) {
    if (!isset($_COOKIE['utm_source'])) {
      setcookie('utm_source', $_GET['utm_source']);
    }
  }
}

//Call the above function
add_action( 'init', 'namespace_set_source' );


//Return the utm_source to Gravity Forms
function namespace_populate_source($form) {
  $_POST['input_8'] = $_COOKIE['utm_source'];
}

//Call the above function
add_action('gform_pre_submission_1', 'namespace_populate_source');

I would recommend using gform_field_value_parameter_name for this. That can definitely be used to populate a field in the form, using your own parameter name. Try it like that and share that code with us in the same way. Thank you.

Hi Chris,

Thanks for the response. I have tried that version before and it had the same result. Here is the code for that:

//Save the utm_source parameter as a session cookie
function namespace_set_source() {
  if (isset($_GET['utm_source'])) {
    if (!isset($_COOKIE['utm_source'])) {
      setcookie('utm_source', $_GET['utm_source']);
    }
  }
}

//Call the above function
add_action( 'init', 'namespace_set_source' );


//Return the utm_source to Gravity Forms
function namespace_populate_source($value) {
  return $_COOKIE['utm_source'];
}

//Call the above function
add_filter('gform_field_value_source', 'namespace_populate_source');

This should work. I would add some logging to this to see when the cookie is set and what the function returns when you run it. Try this version with some additional logging statements:

//Save the utm_source parameter as a session cookie
function namespace_set_source() {
  GFCommon::log_debug( __METHOD__ . '(): running.' );
  if (isset($_GET['utm_source'])) {
    GFCommon::log_debug( __METHOD__ . '(): There is a GET utm_source.' );
    if (!isset($_COOKIE['utm_source'])) {
      GFCommon::log_debug( __METHOD__ . '(): There is no cookie utm_source.' );
      setcookie('utm_source', $_GET['utm_source']);
    }
  }
}

//Call the above function
add_action( 'init', 'namespace_set_source' );


//Return the utm_source to Gravity Forms
function namespace_populate_source($value) {
  GFCommon::log_debug( __METHOD__ . '(): Cookies => ' . print_r( $_COOKIE['utm_source'], true ) );
  return $_COOKIE['utm_source'];
}

//Call the above function
add_filter('gform_field_value_source', 'namespace_populate_source');

Oh, you will need to be sure that logging is enabled. You can do that on the Forms > Settings page under Logging (set the radio button to Yes.) Then on the Forms > Settings > Logging page, enable logging for Gravity Forms for all messages. The code I added won’t be very useful without logging enabled!

Also, caching might be an issue. Many hosts do aggressive caching these days that make setting/fetching cookies unreliable on cached pages.

If you think this might be impacting you, give our (Gravity Wiz) Cache Busting snippet/plugin a try:

To install as a plugin, just download, zip, and upload via WP admin. Once activated, just add the cachebuster parameter to your [gravityforms] shortcode.

[gravityforms id="123" cachebuster="true"]

1 Like

Hi Chris & David,

Thanks for the help.

Chris, I tried using your logging code, but it doesn’t return much. When I view the log, it tells me that the first function is running, but it doesn’t mention the second function at all. The code (minus sensitive bits) is attached below.

David, unfortunately, the cache busting approach didn’t work, but I didn’t think it would since I had disabled all caching to run this test.

2018-12-10 22:11:48.122214 - DEBUG --> namespace_set_source(): running. 2018-12-10 22:11:48.256704 - DEBUG --> GFCommon::post_to_manager(): endpoint: https://www.gravityhelp.com/wp-content/plugins/gravitymanager/api.php?op=upgrade_message&key=[api-key] 2018-12-10 22:11:48.457597 - DEBUG --> GFCommon::log_remote_response(): code: 200; body: <!--GFM--> 2018-12-10 22:11:49.852075 - DEBUG --> namespace_set_source(): running.

Can you send me the system report or the Gravity Forms Core log to chris@rocketgenius.com? We’ll need to see more.

Based on the limited amount of the log shared, this logical check is failing, so none of the code is being executed. This must not be true when it is reached in your function:

if (isset($_GET['utm_source'])) {

The way I read it, you don’t do anything in case it’s not set: you are only doing something when it is set.

I just sent you the log file. Thank you!

Hi Dan. I can’t find the email with the log file. Can you please resend it to chris@rocketgenius.com

Thank you.

Hi Chris. Sure thing. I just resent it. It will come from googlepartners@c1-partners.com

I replied to your email. Thank you.

© 2008 - 2021. Gravity Forms is a project by Rocketgenius Inc.