Yes, the $mime_types array must contain every MIME type that you want to allow to be uploaded.
You could restrict how many files are uploaded at once (but they could return to add more later), or limit images per object (post or user). At once limitation would involve modifying the image uploader. Limits per object could be done through the ‘wp_insert_attachment_data’ filter. Your callback would check a stored count in post/user meta and die if the quota had been surpassed. If not, increment the count.
Thank you for your answer.
However, I could not understand exactly what to write in this part of the code:
if($roles[0]==’Customer’){
$mime_types = array(
//?
);
return $mime_types;
}
For example, the iphone uses the HEIC file extension.
I couldn’t find the relevant extension on this site .
So, is the following definition sufficient? So the user will be able to upload “images only”.
function wpse_restrict_mimes($mime_types){
//Get current user
$user = wp_get_current_user();
//Obtaining the role
$roles = ( array ) $user->roles;
if($roles[0]=='Customer'){
//Forbiden ALL
unset( $mimes );
$mime_types = array(
//Image only
$mimes['svg'] = 'image/svg+xml';
$mimes['jpg'] = 'image/jpeg, image/pjpeg
$mimes['jpeg'] = 'image/jpeg, image/pjpeg
$mimes['png'] = 'image/png
);
return $mime_types;
}
}
add_filter('upload_mimes', 'wpse_restrict_mimes');
-
This reply was modified 4 years, 6 months ago by
loopforever.
-
This reply was modified 4 years, 6 months ago by
loopforever.
-
This reply was modified 4 years, 6 months ago by
loopforever.
With that code, customers can only upload the files listed under //Image only. If you want to also allow HEIC files, add applicable extensions (‘heif’ and ‘heic’) to the list along with the relevant MIME type (image/heif for both I believe).
Your code has not balanced the quotes. Every open quote needs a close quote, for example:
$mimes['jpg'] = 'image/jpeg, image/pjpeg',
Also, every array element must be completed with a terminal comma , before starting the next line. The final element can have a comma after it, but it’s not required. I like to include it because it makes future code maintenance easier.
I’m unsure of the consequences of listing two MIME types for one extension. I think it will be problematic. I recommend using just the one until customer JPEG uploads are confirmed to work as expected. Once all is working, you can try adding a second back in to see what effect it has, if any.
Ensure the customer role really includes upper case ‘C’ in its slug. They are commonly all lower case, but YMMV. Some systems don’t care about case, others do.
I understood. Thank you so much @bcworkz