Skip to content

Commit 0027381

Browse files
authored
gprf-clear-specific-field-values-on-reload.php: Fixed an issue where checkbox and file upload field values were not cleared as expected when reloading the form.
1 parent 084abdc commit 0027381

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

gp-reload-form/gprf-clear-specific-field-values-on-reload.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,41 @@
99
*/
1010
// Update "123" to your form ID.
1111
add_filter( 'gprf_disable_dynamic_reload_123', function( $return ) {
12-
function gprf_clear_specific_field_values( $return ) {
13-
// Update "3" to the ID of the field for whose value you would like to clear.
14-
$_POST['input_3'] = '';
15-
remove_filter( 'gform_pre_render', 'gprf_clear_specific_field_values' );
16-
return $return;
17-
}
18-
add_filter( 'gform_pre_render', 'gprf_clear_specific_field_values' );
12+
13+
$form_id = 123;
14+
$fields_to_clear = array( 2, 3, 4 ); // array of field IDs to clear
15+
16+
add_filter( 'gform_pre_render_' . $form_id, function( $form ) use ( $fields_to_clear ) {
17+
18+
foreach ( $fields_to_clear as $field_id ) {
19+
$field = GFAPI::get_field( $form, $field_id );
20+
21+
if ( ! $field ) {
22+
continue; // skip if field not found
23+
}
24+
25+
if ( $field->type === 'checkbox' ) {
26+
foreach ( $_POST as $key => $value ) {
27+
if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
28+
$_POST[ $key ] = '';
29+
}
30+
}
31+
} elseif ( $field->type === 'fileupload' ) {
32+
if ( isset( $_POST['gform_uploaded_files'] ) ) {
33+
$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
34+
if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
35+
unset( $uploaded_files[ "input_{$field_id}" ] );
36+
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
37+
}
38+
}
39+
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
40+
} else {
41+
$_POST[ "input_{$field_id}" ] = '';
42+
}
43+
}
44+
45+
return $form;
46+
} );
47+
1948
return $return;
2049
} );

0 commit comments

Comments
 (0)