Is it possible to restrict uploads file types for selected WordPress role? Thanks to WordPress developers, Yes.
There is a special Upload Mimes Filter ‘upload_mimes’, which allows to change what file types are allowed to upload to your WordPress.
Let’s use the example when we need to allow to ‘author’ role upload/attach to the posts just PDF files.
Your steps are simple:
1) create .php file, e.g. myme_types.php;
2) insert this code there:
add_filter('upload_mimes','restrict_mimes_for_author'); function restrict_mimes_for_author($mimes) { if (!current_user_can('author')) { return; } $mimes = array('pdf' => 'application/pdf'); return $mimes; } |
3) Save changes and place this file to the wp-content/mu-plugins folder.
Thats’ it. Now, in case your author try to upload any file type except PDF he will get this error message from WordPress:
You may download ready to use file myme_types.php here.
If you need to extend allowed file types, add them to the $mimes array above as additional elements. For example, to restrict uploads file types to PDF and JPEG only, use this array:
$mimes = array( 'pdf' => 'application/pdf', 'jpg|jpeg' => 'image/jpeg' ); |
The list of mime types is available here.