Home / functions.php / Restrict file type uploads by user name
  • How to Restrict file type uploads by user name

    restrict-file-type-uploads-by-user-name

    Adding this snippet to the functions.php of your worpress theme will let you restrict file uploads by type for specific users only. When a username is not in the users array they will only be able to upload jpg, and gif images.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    add_filter('upload_mimes','restrict_mime');
    function restrict_mime($mimes) {
        global $current_user;
        get_currentuserinfo();
        // change users in list
        $users = array(
                                  "ryan",
                                  "steven",
                                  "larry",
                                  "jerry"
                                );
        if (!in_array($current_user->user_login, $users)) {
    	$mimes = array(
    	                'jpg|jpeg|jpe' => 'image/jpeg',
    	                'gif' => 'image/gif',
    	);
    	}
    	return $mimes;
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    upload_mimes, get_currentuserinfo, add_filter,