Limit How Many Files Non-Administrators Can Upload per Hour
Caps uploads at 30 files per hour for each user who is not an administrator, which limits the damage a compromised author account or a buggy front-end upload form can do.
Opens WPIDE on
Code
// Uploads allowed per user per hour.
$wpide_upload_hourly = 30;
add_filter( 'wp_handle_upload_prefilter', function ( $file ) use ( $wpide_upload_hourly ) {
if ( ! empty( $file['error'] ) || ! is_user_logged_in() || current_user_can( 'manage_options' ) ) {
return $file;
}
$key = 'wpide_uploads_' . get_current_user_id();
$count = (int) get_transient( $key );
if ( $count >= $wpide_upload_hourly ) {
$file['error'] = sprintf( 'You have reached the limit of %d uploads per hour. Please try again later.', $wpide_upload_hourly );
return $file;
}
set_transient( $key, $count + 1, HOUR_IN_SECONDS );
return $file;
}, 20 );
Notes
Raise $wpide_upload_hourly for teams that upload large batches of photos.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.