Description
gform_submission_files_pre_save_field_value
allows filtering (e.g., renaming) of the submission files before they are saved to the form uploads folder and entry.
Usage
add_filter( 'gform_submission_files_pre_save_field_value', 'my_function', 10, 4 );
Parameters
Parameter | Type | Description |
---|---|---|
$files | array | Array of submission files. See GF_Field_FileUpload::get_submission_files() for structure. |
$field | GF_Field_FileUpload | The File Upload field the files are for. |
$entry | array | The entry currently being saved. Only fields located before the current field are included. |
$form | array | The form currently being processed. |
Examples
Rename uploaded files to include the entry and field IDs.
add_filter( 'gform_submission_files_pre_save_field_value', function ( $files, $field, $entry ) {
$entry_id = absint( rgar( $entry, 'id' ) );
foreach ( $files['existing'] as &$existing_file ) {
$existing_file['uploaded_filename'] = sprintf(
'entry-%d-field-%d-%s',
$entry_id,
$field->id,
$existing_file['uploaded_filename']
);
}
foreach ( $files['new'] as &$new_file ) {
$new_file['name'] = sprintf(
'entry-%d-field-%d-%s',
$entry_id,
$field->id,
$new_file['name']
);
}
return $files;
}, 10, 3 );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
The filter is located in GF_Field_FileUpload::filter_submission_files_pre_save()
in includes/fields/class-gf-field-fileupload.php
Since
Gravity Forms 2.9.18