|
9 | 9 | */
|
10 | 10 | // Update "123" to your form ID.
|
11 | 11 | 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 | + |
19 | 48 | return $return;
|
20 | 49 | } );
|
0 commit comments