In Forminator, restrict upload of certain file types and sizes

I want to restrict uploads based on file type and file size using Forminator.

  • Ash

    Hello Vlad

    Please try the following code:

    add_filter( 'forminator_file_upload_allow', function( $res, $field_name, $file_name, $valid ) {
    	$allowed_extension = array( 'jpg', 'bmp' );
    	$size = '100000'; // In bytes
    
    	$ext = $_FILES[ $field_name ]['name'];
    	$ext = pathinfo( $ext, PATHINFO_EXTENSION );
    
    	if( ! in_array( $ext, $allowed_extension ) ) return false;
    	if( $_FILES[ $field_name ]['size'] > $size ) return false;
    
    	return $res;
    
    }, 99, 4 );

    Here, in $allowed_extension array you need to mention the extension you allow. And in $size variable, the maximum size you allow in bytes. Please note that, forminator use the default restriction used by wordpress. So, if you want to restrict in wordpress level, then you don't need the above code.

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.