Skip to content
Advertisement

How to decrease file size upload for non-admin users?

My site allows upload file size of up to 256 MB but I would like to limit my site users to only 1 MB max and keep the maximum 256 MB for the admins. All solutions I found only show how to increase the upload limit, but not decrease for specific roles.

I tried the following but it didn’t work:

function increase_upload_size_limit( $limit ) {
  if ( ! current_user_can( 'manage_options' ) ) {
    $limit = 1048576; // 1 MB
  }
  return $limit;
}
add_filter( 'upload_size_limit', 'increase_upload_size_limit' );

Advertisement

Answer

This seems like what you’re asking for is just an if/else?

function increase_upload_size_limit( $limit ) {
  if ( ! current_user_can( 'manage_options' ) ) {
    $limit = 1048576; // 1 MB
  } else {
      $limit = 268435456;
  }
    return $limit;
}
add_filter( 'upload_size_limit', 'increase_upload_size_limit' );
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement