• Resolved loopforever

    (@loopforever)


    Hello,
    I want to allow users with the Customer role to upload “Image only”. I don’t want it to load other types of files. So I want to restrict it. However, I could not define the mine type. Should I define each file type separately? For example, a client should be able to upload an image file with the HEIF extension. Should I define it separately?
    I also want to set a limit on qty. For example, it should be able to add up to 3 images. How can do it ?

    My code is below:

    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'){
        $mime_types = array(
            //?
        );
        return $mime_types;
    }
    }
    add_filter('upload_mimes', 'wpse_restrict_mimes');
    • This topic was modified 3 years, 4 months ago by loopforever.
    • This topic was modified 3 years, 4 months ago by loopforever.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter loopforever

    (@loopforever)

    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 3 years, 3 months ago by loopforever.
    • This reply was modified 3 years, 3 months ago by loopforever.
    • This reply was modified 3 years, 3 months ago by loopforever.
    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter loopforever

    (@loopforever)

    I understood. Thank you so much @bcworkz

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Restrict in WordPress’ is closed to new replies.