Adding New FileTypes To WordPress Media Library

Sharing is caring!

WordPress CMS has a set of pre-defined file types that allow you to upload in the media library. This is due to a great security feature of WordPress CMS.

But there may be many times where you’d like to add new file types that are not in the list of allowed by WordPress, or some time you need to only allow a fewer extensions to be uploaded.

This can be done by using a small hook of WordPress.
If you’d like to add a specific file type that can be uploaded to WordPress via the media library, you can insert this PHP code in your theme functions.php file:


function tar_mime_types($mime_types){

$mime_types[‘tar’] = ‘application/x-tar’; //Adding tar extension

return $mime_types;
}
add_filter(‘upload_mimes’, ‘tar_mime_types’, 1, 1);

But if you want to remove any existing file types then you need to do simple unset using this code:

function gif_mime_types($mime_types){

unset($mime_types[‘gif’]); //Removing the gif extension

return $mime_types;
}

add_filter(‘upload_mimes’, ‘gif_mime_types’, 1, 1);

Another way you can also reset the allowed filetypes by creating a new array within the function and returning these values:

[sam id="5" codes="true"]
function reset_myme_types($mime_types){

//New array to reset the allowed filetypes
$mime_types = array(
‘jpg|jpeg|jpe’ => ‘image/jpeg’,
‘gif’ => ‘image/gif’,
‘png’ => ‘image/png’,
‘bmp’ => ‘image/bmp’,
‘tif|tiff’ => ‘image/tiff’
);

return $mime_types;
}
add_filter(‘upload_mimes’, ‘reset_myme_types’, 1, 1);

You can get more file types and their mime type here

Leave a Reply

Your email address will not be published. Required fields are marked *

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange