02 Jul

Adding & Removing Allowed FileTypes To WordPress Media Library

WordPress has a set of restricted filetypes it will allow you to upload via the media library. Whilst this is a great security feature, there may be times where you’d like to add other files that are restricted by default, or maybe even the opposite where you’d only like to allow a few extensions to be uploaded. Fortunately, WordPress makes this dead easy with a small snippet of PHP code.

If you’d like to add or remove a specific filetype that can be uploaded to wordpress via the media library, you can insert this PHP code in your theme functions.php file:

function my_myme_types($mime_types){
	//Adjust the $mime_types, which is an associative array where the key is extension and value is mime type.
	return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

Here is an example of what you can do to add and remove a new filetype (in this example, I’m adding an extension that already exists, but the concept is the same):

function my_myme_types($mime_types){
	$mime_types['avi'] = 'video/avi'; //Adding avi extension
	unset($mime_types['pdf']); //Removing the pdf extension
	return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

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

function my_myme_types($mime_types){
	//Creating a new array will 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', 'my_myme_types', 1, 1);

If you’d like to see what filetypes are currently supported by wordpress, check out the function get_allowed_mime_types located in the wp-includes/functions.php file.

If you liked this post, why not share it?

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit
  • RSS
  • StumbleUpon
  • Technorati
  • Twitter
  • http://www.domasti.com Ch_aman

    Nice man. Thank You It works :-)

  • http://blog.woosum.net/2010/11/wordpress%ec%97%90-7z%ec%9c%bc%eb%a1%9c-%ec%95%95%ec%b6%95%ed%95%9c-%ed%8c%8c%ec%9d%bc-%ec%98%ac%eb%a6%ac%ea%b8%b0/ Wordpress에 7z으로 압축한 파일 올리기.. at Your wish is my command

    [...] http://netweblogic.com/other/adding-removing-allowed-filetypes-wordpress-media-library/ 를 보시라 그냥 wp-includes/functions.php 파일을 열어서 get_allowed_mime_types 에 적당히 추가해주면 된다. 현재 이를 관리할 수 있는 Plugin은 없는 듯 하다. Tweet [...]

  • Janas

    How do I access the “PHP code in your theme functions.php file”?

  • http://netweblogic.com Marcus

    you probably need FTP to access your theme files on your website, which are located in /wp-content/themes/yourthemename