How To: Add and Remove Allowed File Types in WordPress
Recently I had a client request that we block BMP files from being an allowed file type for uploads in WordPress. As always WordPress provides a very simple way to set what file extensions are allowed to be uploaded. To do this we’ll use the upload_mimes action filter. Add the below code example to your theme’s functions.php file or a custom plugin:
add_filter( 'upload_mimes', 'brad_mime_types' ); function brad_mime_types( $mime_types ){ //remove bmp from allowed types unset( $mime_types['bmp'] ); //return the array of allowed types return $mime_types; }
As you can see we use the add_filter() function to hook the upload_mimes filter. The filter passes a single variable, which is an array of all allowed file types in WordPress. To remove a type we simply use the unset() PHP function to remove the ‘bmp’ value from the array. The final step is to return the array of allowed file types back to WordPress.
That’s it! Using the above code BMP files are no longer allowed for upload in WordPress.