-
Notifications
You must be signed in to change notification settings - Fork 92
gw-custom-modifier-file-upload.php: Added snippet for custom filename modifier for file upload fields.
#1144
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||
| <?php | ||||||||||||
| /** | ||||||||||||
| * Gravity Wizards // Gravity Forms // Custom Modifier for File Upload | ||||||||||||
| * | ||||||||||||
| * This snippet is an example of how to add custom modifiers to a Gravity Forms file upload field. | ||||||||||||
| * | ||||||||||||
| * Instructions: | ||||||||||||
| * | ||||||||||||
| * 1. Add to snippet to site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/. | ||||||||||||
| */ | ||||||||||||
| add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) { | ||||||||||||
| if ( $merge_tag != 'all_fields' && $field->type == 'fileupload' && ! empty( $raw_value ) && $modifier == 'filename' ) { | ||||||||||||
| if ( ! $field->multipleFiles ) { | ||||||||||||
| $value = basename( $raw_value ); | ||||||||||||
| } else { | ||||||||||||
| $file_list = []; | ||||||||||||
| foreach ( json_decode( $raw_value ) as $filepath ) { | ||||||||||||
| $file_list[] = basename( $filepath ); | ||||||||||||
| } | ||||||||||||
| $value = implode( '<br />', $file_list ); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| return $value; | ||||||||||||
| }, 10, 6 ); | ||||||||||||
|
|
||||||||||||
| // For GP Media Library | ||||||||||||
| add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) { | ||||||||||||
| return [ 'filename' ]; | ||||||||||||
|
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 Preserve other modifiers skipped by GPML Don’t clobber existing $skip_modifiers; merge with them to remain compatible with other customizations. -return [ 'filename' ];
+$skip_modifiers = is_array( $skip_modifiers ) ? $skip_modifiers : array();
+$skip_modifiers[] = 'filename';
+return array_values( array_unique( $skip_modifiers ) );📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||
| }, 10, 4 ); | ||||||||||||
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.
🛠️ Refactor suggestion
Escape output, strip query strings, honor $format, and harden JSON handling
Avoid potential XSS by escaping HTML output, remove query strings from URLs, respect text vs HTML output via $format, and guard against invalid JSON for multi-file values.
📝 Committable suggestion
🤖 Prompt for AI Agents