WordPress at a glance

How Allow to Upload Forbidden File Types

WordPress does not allow to upload to the library any type of file you want - there are limitations. Files of non-standard (not allowed, forbidden) types cannot be uploaded because WP has a list of valid file formats, i.e. a whitelist of files mime types.

Sometimes such protection is hindered, and despite the potential danger, it is still necessary to be able to upload files with not allowed types.

Below I show you how to add a file type to the WordPress uploads whitelist.

This is what the error looks like if, for example, you try to upload a file with an extension .svg:

Allow or Deny file types for upload in WordPress

There are several hooks to manage the whitelist of file extensions:

The example below shows how to allow uploading of some file types (.svg, .doc, .djvu) and how to prohibit (remove from the whitelist) others (.mp4a).

add_filter( 'upload_mimes', 'upload_allow_types' );
function upload_allow_types( $mimes ) {
	// allow new types
	$mimes['svg']  = 'image/svg+xml';
	$mimes['doc']  = 'application/msword'; 
	$mimes['woff'] = 'font/woff';
	$mimes['psd']  = 'image/vnd.adobe.photoshop'; 
	$mimes['djv']  = 'image/vnd.djvu';
	$mimes['djvu'] = 'image/vnd.djvu';

	// disable existing
	// unset( $mimes['mp4a'] );

	return $mimes;
}

image/svg+xml - it is a MIME type of svg file.

By analogy, you can allow or deny any other file types, a list of MIME types that are needed for the whitelist can be found at: list of MIME file types.

The process of uploading the file and checking its type

No comments
    Hello, !     Log In . Register