Restrict Mime Types on WordPress Media Upload

WordPress has changed the library in version 3.3 – which I think is an improvement. The restrictions in terms of file types remains and you can control them via hook. So you can limit or extend the file-types. Via two hooks, this is done quickly and there is a notification displayed in your backend which lists the allowed file types.


Screenshot Example for restrive the mime type

The following small Plugin may extend to different roles or authorization objects, so that you can upload, depending on the role, different types of files in the system – current_user_can().

Anyone interested in the currently allowed types, you can return the array of the first function or look into the function get_allowed_mime_types() in wp-includes/functions.php.

Screenshot for Hint about allowed Mime Types

 'application/pdf',
			'doc|docx' => 'application/msword',
		);
		
		return $mime_types;
	}
}

// If the function exists this file is called as post-upload-ui.
// We don't do anything then.
if ( ! function_exists( 'fb_restrict_mime_types_hint' ) ) {
	// add to wp
	add_action( 'post-upload-ui', 'fb_restrict_mime_types_hint' );
	/**
	 * Get an Hint about the allowed mime types
	 * 
	 * @return  void
	 */
	function fb_restrict_mime_types_hint() {
		
		echo '
'; _e( 'Accepted MIME types: PDF, DOC/DOCX' ); } }

Posted

in

by

Comments

2 responses to “Restrict Mime Types on WordPress Media Upload”

  1. […] WPEngineer wird gezeigt, wie man den Upload in die Medienverwaltung von WordPress 3.3 auf bestimmte Medientypen beschränken […]

  2. NIcolas Avatar
    NIcolas

    Very usefull to block users to upload BMP!