Last active
May 31, 2016 09:20
-
-
Save mafsdisseny/8eba2054f3683539e55c36016576deb1 to your computer and use it in GitHub Desktop.
Limit WordPress media uploader maximum upload file size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Limit WordPress media uploader maximum upload file size | |
* Uploading very large images is pointless as they will hardly ever be used at full size. | |
* Crunching larger files takes more memory; larger files take more space too. | |
* @author https://gist.github.com/unfulvio/1ee103a6f86893771b5a | |
* @param mixed $file the uploaded file item to filter | |
* @return array $file the filtered file item with response | |
*/ | |
function limit_upload_file_size( $file ) { | |
$images = 1024; // size in KB (example) | |
$others = 2048; // sizee in KB (example) | |
// exclude admins | |
if ( ! current_user_can( 'manage_options' ) ) : | |
// get filesize of upload | |
$size = $file['size']; | |
$size = $size / 1024; // Calculate down to KB | |
// get imagetype of upload | |
$type = $file['type']; | |
$is_image = strpos( $type, 'image' ); | |
// set sizelimit in kB | |
$image_limit = $images; | |
$others_limit = $others; | |
if ( $is_image == true && $size > $image_limit ) { | |
$file['error'] = sprintf( __( 'WARNING: You should not upload images larger than %d KB. Please reduce the image file size and try again.' ), $images ); | |
} elseif ( $is_image == false && $size > $others_limit ) { | |
$file['error'] = sprintf( __( 'WARNING: You should not upload files larger than %d KB. Please reduce the file size and try again.' ), $others ); | |
} | |
endif; | |
return $file; | |
} | |
add_filter ( 'wp_handle_upload_prefilter', 'limit_upload_file_size', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment