Default Upload Limits for Gravity Forms File Fields
Gives every Gravity Forms file upload field a safe list of allowed file types and a maximum file size when the field does not set its own.
Opens WPIDE on
Code
$wpide_gf_upload_ext = 'pdf,doc,docx,jpg,jpeg,png'; // Allowed extensions.
$wpide_gf_upload_mb = 5; // Maximum size per file in MB.
$wpide_gf_upload_defaults = function ( $form ) use ( $wpide_gf_upload_ext, $wpide_gf_upload_mb ) {
foreach ( $form['fields'] as $field ) {
if ( 'fileupload' !== $field->type ) {
continue;
}
if ( empty( $field->allowedExtensions ) ) {
$field->allowedExtensions = $wpide_gf_upload_ext;
}
if ( empty( $field->maxFileSize ) ) {
$field->maxFileSize = $wpide_gf_upload_mb;
}
}
return $form;
};
add_filter( 'gform_pre_render', $wpide_gf_upload_defaults );
add_filter( 'gform_pre_validation', $wpide_gf_upload_defaults );
add_filter( 'gform_pre_submission_filter', $wpide_gf_upload_defaults );
Notes
Values set in the field's own settings always win; the server's upload limit must also allow the size.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.