• Hi, I restrict uploads to specified file types only (jpg, gif, png) using a code I added to my functions.php:

    add_filter('upload_mimes','restrict_mime');
    function restrict_mime($mimes) {
    $mimes = array(
                    'jpg|jpeg|jpe' => 'image/jpeg',
                    'gif' => 'image/gif',
    				'png' => 'image/png',
    );
    return $mimes;
    }

    Now I would like to also allow mp3 files to be uploaded but only by admin and editor roles. I have tried some code by my own with no success at all, if you know how to do it, thank you very much for your help! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    First, you’d get the current user into $current_user. If that is non-zero, then look at $current_user->roles to see if the user has one of the roles. If so, then add the necessary mime type(s) to support MP3 uploads.

    Thread Starter Alkorr

    (@alkorr)

    Hi Steven, thank you for your reply! In fact I don’t code at all but I’m trying ??

    Here is what I tried to do following your advice but I’m afraid it doesn’t work. It may seems easy to you but please be indulgent:

    add_filter('upload_mimes','restrict_mime');
    function restrict_mime($mimes) {
    $mimes = array(
                    'jpg|jpeg|jpe' => 'image/jpeg',
                    'gif' => 'image/gif',
    		'png' => 'image/png',
    );
    
    if ( ! current_user_can('administrator,editor') ) {
    $mimes = 'mp3',
    }
    return $mimes;
    }

    Thanks again!

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’re just resetting the entire array. That code should be $mimes = array('mp3'=>'whatever the mime type string is'),

    Thread Starter Alkorr

    (@alkorr)

    Ok, I think I found what was missing regarding the mime type string. I also changed current_user_can for an array:

    add_filter('upload_mimes','restrict_mime');
    function restrict_mime($mimes) {
    $mimes = array(
                    'jpg|jpeg|jpe' => 'image/jpeg',
                    'gif' => 'image/gif',
    		'png' => 'image/png',
    );
    
    if ( ! current_user_can( array( 'editor', 'administrator' ) ) ) {
    $mimes = array('mp3'=>'audio/mp3', 'audio/mpeg3', 'audio/x-mpeg-3'),
    }
    return $mimes;
    }

    I hope this one works better ??

    • This reply was modified 4 years, 9 months ago by Alkorr.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Allow uploads to specified file types by roles’ is closed to new replies.