Default Upload Limits for Contact Form 7 File Fields
Gives every Contact Form 7 file field a safe default list of allowed file types and a maximum size, unless the field already sets its own.
Opens WPIDE on
Code
$wpide_cf7_upload_types = 'pdf|doc|docx|jpg|jpeg|png'; // Allowed extensions, separated by |.
$wpide_cf7_upload_limit = '5mb'; // Maximum size per file (kb or mb).
add_filter( 'wpcf7_form_tag', function ( $tag ) use ( $wpide_cf7_upload_types, $wpide_cf7_upload_limit ) {
if ( empty( $tag->basetype ) || 'file' !== $tag->basetype ) {
return $tag;
}
$options = (array) $tag->options;
if ( ! $tag->has_option( 'filetypes' ) ) {
$options[] = 'filetypes:' . $wpide_cf7_upload_types;
}
if ( ! $tag->has_option( 'limit' ) ) {
$options[] = 'limit:' . $wpide_cf7_upload_limit;
}
$tag->options = $options;
return $tag;
} );
Notes
Fields with their own filetypes: or limit: options keep them; the server's upload_max_filesize 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.