Using WordPress ‘mime_types’ PHP filter

The mime_types filter in WordPress allows you to add new mime types and their corresponding file extensions to the list of allowed mime types for file uploads.

Usage

add_filter('mime_types', 'my_custom_mime_types');
function my_custom_mime_types($wp_get_mime_types) {
    // your custom code here
    return $wp_get_mime_types;
}

Parameters

  • $wp_get_mime_types (string[]): An array of mime types, where the key is the file extension regex and the value is the mime type.

More information

See WordPress Developer Resources: https://developer.wordpress.org/reference/hooks/mime_types/

Examples

Add WebP support

Add the WebP image format to the list of allowed mime types.

add_filter('mime_types', 'add_webp_mime_type');
function add_webp_mime_type($wp_get_mime_types) {
    $wp_get_mime_types['webp'] = 'image/webp';
    return $wp_get_mime_types;
}

Add SVG support

Allow SVG files to be uploaded by adding the appropriate mime type.

add_filter('mime_types', 'add_svg_mime_type');
function add_svg_mime_type($wp_get_mime_types) {
    $wp_get_mime_types['svg'] = 'image/svg+xml';
    return $wp_get_mime_types;
}

Add support for custom font formats

Allow uploading custom font formats like WOFF and WOFF2.

add_filter('mime_types', 'add_custom_font_mime_types');
function add_custom_font_mime_types($wp_get_mime_types) {
    $wp_get_mime_types['woff'] = 'font/woff';
    $wp_get_mime_types['woff2'] = 'font/woff2';
    return $wp_get_mime_types;
}

Add JSON file support

Allow JSON files to be uploaded by adding the corresponding mime type.

add_filter('mime_types', 'add_json_mime_type');
function add_json_mime_type($wp_get_mime_types) {
    $wp_get_mime_types['json'] = 'application/json';
    return $wp_get_mime_types;
}

Add support for custom video format

Allow uploading a custom video format, such as OGV.

add_filter('mime_types', 'add_ogv_mime_type');
function add_ogv_mime_type($wp_get_mime_types) {
    $wp_get_mime_types['ogv'] = 'video/ogg';
    return $wp_get_mime_types;
}