-
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 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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||
| <?php | ||||||||||||||||
| /** | ||||||||||||||||
| * Gravity Wizards // Gravity Forms // Filename Modifier for File Upload | ||||||||||||||||
| * | ||||||||||||||||
| * This snippet is an example of how to add filename 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' ]; | ||||||||||||||||
| }, 10, 4 ); | ||||||||||||||||
|
Comment on lines
+27
to
+29
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. Don’t clobber existing GPML skip modifiers Returning only ['filename'] discards previously skipped modifiers from other plugins/snippets. Append instead of replace. -add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) {
- return [ 'filename' ];
-}, 10, 4 );
+add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) {
+ $skip_modifiers[] = 'filename';
+ return array_values( array_unique( $skip_modifiers ) );
+}, 10, 4 );📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
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
Harden modifier handling, decoding, and output escaping (avoid warnings and potential XSS)
vs newline).
🤖 Prompt for AI Agents