Skip to content

Instantly share code, notes, and snippets.

@MikevHoenselaar
Last active January 20, 2017 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikevHoenselaar/ea04871ea188fec56009 to your computer and use it in GitHub Desktop.
Save MikevHoenselaar/ea04871ea188fec56009 to your computer and use it in GitHub Desktop.
Render persistence data before form output changed to work with populated fields.
```php
// Render persistence data before form output
add_filter("gform_pre_render", "ri_pre_populate_the_form");
function ri_pre_populate_the_form($form) {
if (gfdp_is_persistent($form)) {
$current_page = GFFormDisplay::get_current_page($form["id"]);
if ($current_page == 1) {
$option_key = ri_getFormOptionKeyForGF($form);
if (get_option($option_key)) {
$persistent_info = json_decode(get_option($option_key));
//start new code
// For every field in form we have to check if populate is active, if so, grab inputName value (=$_GET value). If its not empty use that for default field value.
foreach ($form['fields'] as $_field) :
if (isset($_field['allowsPrepopulate']) && $_field['allowsPrepopulate']) :
$get_key = $_field->inputName;
if (isset($_GET[$get_key]) && !empty($get_key)) :
$field_id = ($_field->id) ? $_field->id : '' ;
if ($field_id && isset($persistent_info->{'input_'.$field_id})) :
$persistent_info->{'input_'.$field_id} = sanitize_text_field($_GET[$get_key]);
endif;
endif;
endif;
endforeach;
// end new code
$persistent_info = json_encode($persistent_info);
$_POST = json_decode($persistent_info, true);
}
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment