-
Notifications
You must be signed in to change notification settings - Fork 92
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.
#1154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,12 +9,41 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Update "123" to your form ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'gprf_disable_dynamic_reload_123', function( $return ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function gprf_clear_specific_field_values( $return ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Update "3" to the ID of the field for whose value you would like to clear. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $_POST['input_3'] = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| remove_filter( 'gform_pre_render', 'gprf_clear_specific_field_values' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'gform_pre_render', 'gprf_clear_specific_field_values' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $form_id = 123; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $fields_to_clear = array( 2, 3, 4 ); // array of field IDs to clear | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'gform_pre_render_' . $form_id, function( $form ) use ( $fields_to_clear ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ( $fields_to_clear as $field_id ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $field = GFAPI::get_field( $form, $field_id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! $field ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; // skip if field not found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( $field->type === 'checkbox' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ( $_POST as $key => $value ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( strpos( $key, "input_{$field_id}" ) === 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $_POST[ $key ] = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } elseif ( $field->type === 'fileupload' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( isset( $_POST['gform_uploaded_files'] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unset( $uploaded_files[ "input_{$field_id}" ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Harden file upload clearing: unslashing, type checks, and static array init. Avoids edge-case warnings and ensures the static cache is always an array. Apply this diff: - } elseif ( $field->type === 'fileupload' ) {
- if ( isset( $_POST['gform_uploaded_files'] ) ) {
- $uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
- if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
- unset( $uploaded_files[ "input_{$field_id}" ] );
- $_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
- }
- }
- GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
+ } elseif ( $field->type === 'fileupload' ) {
+ if ( isset( $_POST['gform_uploaded_files'] ) ) {
+ $uploaded_files = json_decode( wp_unslash( $_POST['gform_uploaded_files'] ), true );
+ if ( is_array( $uploaded_files ) && isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
+ unset( $uploaded_files[ "input_{$field_id}" ] );
+ $_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
+ }
+ }
+ if ( ! is_array( GFFormsModel::$uploaded_files ) ) {
+ GFFormsModel::$uploaded_files = array();
+ }
+ if ( ! isset( GFFormsModel::$uploaded_files[ $form['id'] ] ) || ! is_array( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
+ GFFormsModel::$uploaded_files[ $form['id'] ] = array();
+ }
+ GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $_POST[ "input_{$field_id}" ] = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $form; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: prefix match can clear the wrong fields (e.g., field 2 matches input_20.*).
Use an exact-key or “dot-delimited” boundary check for checkbox inputs.
Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents