Media PHP Beginner

Set a Smaller Upload Size Limit for Some Roles

Limit authors and contributors to 2 MB uploads while administrators keep the server's full limit.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 7 lines
add_filter( 'upload_size_limit', function ( int $bytes ): int {
	if ( current_user_can( 'manage_options' ) ) {
		return $bytes;
	}

	return min( $bytes, 2 * MB_IN_BYTES );
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Writers often upload photos straight from their camera at 10 MB or more. That fills your storage and slows pages. This snippet sets a lower limit for everyone except administrators.

How it works

WordPress uses upload_size_limit to decide the largest file it accepts, and shows that limit in the uploader. The snippet returns 2 MB for non-administrators, or the server’s own limit if that’s already smaller.

Things to watch

  • This can only lower the limit. To raise it, change upload_max_filesize and post_max_size in your PHP settings or ask your host.
  • Change 2 * MB_IN_BYTES to any size you like.

How to test it

Log in as an author and open Media → Add New. The “Maximum upload file size” should say 2 MB.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only