Default Upload Limits for WPForms File Fields
Gives every WPForms file upload field a safe list of allowed file types and a maximum size when the field does not set its own.
Opens WPIDE on
Code
$wpide_wpf_upload_ext = 'pdf,doc,docx,jpg,jpeg,png'; // Allowed extensions.
$wpide_wpf_upload_mb = 5; // Maximum size in MB.
$wpide_wpf_upload_defaults = function ( $form_data ) use ( $wpide_wpf_upload_ext, $wpide_wpf_upload_mb ) {
if ( empty( $form_data['fields'] ) || ! is_array( $form_data['fields'] ) ) {
return $form_data;
}
foreach ( $form_data['fields'] as $id => $field ) {
if ( 'file-upload' !== $field['type'] ) {
continue;
}
if ( empty( $field['extensions'] ) ) {
$form_data['fields'][ $id ]['extensions'] = $wpide_wpf_upload_ext;
}
if ( empty( $field['max_size'] ) ) {
$form_data['fields'][ $id ]['max_size'] = $wpide_wpf_upload_mb;
}
}
return $form_data;
};
add_filter( 'wpforms_frontend_form_data', $wpide_wpf_upload_defaults );
add_filter( 'wpforms_process_before_form_data', $wpide_wpf_upload_defaults );
Notes
Applies to the File Upload field available in WPForms Pro and newer Lite versions; values set on the field always win.
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.