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