Getting cookie value passed into hidden field - 2024

add_filter( 'gform_field_value_device', 'populate_device' );
function populate_device( $value ) {
    if (isset($_COOKIE['device'])){
            return $_COOKIE['device'];
    }
    else{}

This worked for years.
But recently stop working and the cookies value is not populating in hidden fields.
I can confirm that cookie is created and has a value.

Anybody with similar issue and possible solution to test?

Thank you

Would PHP version influence?

Current the server is with PHP 8.2.16

Any ideas or suggestions are very well welcome.

Thank you

I investigated deeper and almost with 100% certain this stop working after implementation of Google Consent Mode. Not sure why. in the tests I gave full consent on cookies, cookies are created but it not populate the value in the hidden fields.

any ideas?
It’s happening with someone else?

thank you

No input from community?

I tried a lot of things in last days but didn’t found what can be causing this.

Currently the value in the hidden fields are being populated but not by the values that cookies have, what makes it more strange.

for example:

add_filter( ‘gform_field_value_device’, ‘populate_device’ );
function populate_device( $value ) {
if (isset($_COOKIE[‘device’])){
return $_COOKIE[‘device’];
}
else{}
}

Google console shows that value for cookie device is “m” but gravity forms send lead with value “computer”.

any ideas?

The page where the form is embedded is most likely cached. For fields to be populated with the latest values by features like dynamic population and default values, the page needs to be excluded from caching.

Hello Richard,

Thank you for your suggestions.

I use WP Rocket. Always used and it worked in the past. The cookies values populated in the hidden fields of gravity forms without any issue even with WP Rocket caching and optimizing.

I did a test today.
I excluded a URL from WP Rocket cache.
Clean cache in backend of worpress, web browser and server.

The cookies were created and have values. when submit form the hidden fields didn’t get populated by the cookies values. it’s strange.

Any ideas?

Population occurs before the form is displayed, so if the cookie is only being set once the page is displayed, that is too late. In that scenario, you could use the gform_pre_submission hook to add the value to the $_POST during submission.

Hello Richard,
Thanks for you suggestions.
Cookies were created to keep values when visitor navigates between pages, so in this tests the cookies and values already exist before page with form is loaded. But i would like to test it any way.
I have doubts where I should use this line of code

add_action( 'gform_pre_submission' , 'pre_submission' );

before this?

add_filter( ‘gform_field_value_device’, ‘populate_device’ );
function populate_device( $value ) {
if (isset($_COOKIE[‘device’])){
return $_COOKIE[‘device’];
}
else{}

after?

I made some tests, but form didn’t submitted. just stayed like in a loop without going to thank you page or send data,

Thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.