Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created December 9, 2022 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/ebbb51695a7056cb589aaa6a323e1240 to your computer and use it in GitHub Desktop.
Save ideadude/ebbb51695a7056cb589aaa6a323e1240 to your computer and use it in GitHub Desktop.
Limit non-admin/editor file uploads to 2mb with WordPress
<?php
/**
* Limit non-admin/editor file uploads to 2mb
* We check the upload_files capability,
* so anyone with access to the Media Library
* can upload files at the PHP/server set max.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_upload_size_limit_for_non_admins( $size ) {
if ( ! current_user_can( 'upload_files' ) ) {
$size = 2 * 1024; // 2 megabytes
}
return $size;
}
add_filter( 'upload_size_limit', 'my_upload_size_limit_for_non_admins' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment