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.
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.
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.
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.