Home » How to Allow Upload of Custom MIME Types in WordPress

How to Allow Upload of Custom MIME Types in WordPress

Estimated Reading Time: 2 min

Rate this post

Blog Post: How to Allow Upload of Custom MIME Types in WordPress

WordPress restricts the types of files you can upload by default for security reasons. However, there are times when you may need to allow additional file types, such as SVGs, JSON files, or other custom MIME types. This can be easily achieved by adding a custom function to your theme’s functions.php file.

Why Enable Custom MIME Types?

Custom MIME types allow you to upload files that are not supported by WordPress out of the box. This is particularly useful for developers, designers, or anyone dealing with specific file formats.

How to Add Custom MIME Types

Here’s a step-by-step guide:

1.Access Your Theme’s functions.php Fils
Open your WordPress dashboard and navigate to Appearance > Theme Editor. Locate and open the functions.php file of your active theme.

2.Add the Custom Function
Insert the following code snippet into the file.





/* ------------------------------------------------------------------------- *
 * Allow to upload custom_mime_types files.
/* ------------------------------------------------------------------------- */

function custom_mime_types( $mimes ) {
     
    // New allowed mime types.
    $mimes['aac']  = 'audio/aac'; //Adding audio acc files
	$mimes['ogv']  = 'audio/ogg'; //Adding audio ogg files
    $mimes['svg'] = 'image/svg+xml'; // Allow SVG files
    $mimes['webp'] = 'image/webp';   // Allow WebP images
    $mimes['json'] = 'application/json'; // Allow JSON files
    $mimes['svgz'] = 'image/svg+xml'; //Adding svgz extension
	$mimes['rar'] = 'application/vnd.rar'; //Adding rar extension
	$mimes['zip'] = 'application/zip'; //Adding zip extension
	$mimes['woff'] = 'font/woff'; //Adding woff extension
	$mimes['gif'] = 'image/gif'; //Adding gif extension
	$mimes['apk'] = 'application/vnd.android.package-archive'; //Adding apk files
    $mimes['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files
    $mimes['wmv'] = 'video/x-ms-wmv'; //Adding wmv video files
 
    // Optional. Remove a mime type.
    unset( $mimes['exe'] );
 
    return $mimes;
}
 
add_filter( 'upload_mimes', 'custom_mime_types', 1, 1);


/* ------------------------------------------------------------------------- *
 * End upload custom_mime_types files.
/* ------------------------------------------------------------------------- */




  

3.Save the Changes
Once you save the file, the custom file types will be allowed for upload.

Important Notes:

  • Be cautious when enabling file types, as certain formats can pose security risks.
  • Always test the functionality on a staging site before implementing it on a live site.

By following these steps, you can easily extend WordPress’s upload capabilities to suit your needs.


Tags: WordPress, functions.php, upload MIME types, custom MIME types, allow SVG uploads, WordPress tips, WordPress development, upload files, MIME types function, WordPress customization, upload restrictions, custom file types, WordPress code, functions file, developer tips


Discover more from Be-smart

Subscribe to get the latest posts sent to your email.

Photo of author

Flora

How to Allow Upload of Custom MIME Types in WordPress

Published

I am Flora, a www passionate dedicated to sharing insights and inspiration for living a fulfilling life. With a good background in www, I aim to empower others people to truly and genuinely acknowledge them when they do admirable things, big and small.

Leave a Reply