8

I am searching online and all I can see is how to increase file size for media upload, which I know how to do with php.ini, but what I need to do is limit the file size for media upload only.

The client and his associates have trouble with understanding: Please do not upload images that are bigger than 1MB because your site will load forever.

They keep uploading images that are over 8 MB in size, and the whole site takes over 30 sec to load. It's horrendous.

So I was thinking - if it's possible to limit the image upload to 1 MB or so without affecting the general upload_max_filesize which will influence the ability to upload themes and plugins (and I don't want that to happen).

Any idea if this can be done?

4 Answers 4

12

Yes, you can use the wp_handle_upload_prefilter that allows you to stop the uploading process if a specific condition is not accomplished.

In your case, you could try this code snippet:

function limit_upload_size( $file ) {

    // Set the desired file size limit
    $file_size_limit = 1024; // 1MB in KB

    // exclude admins
    if ( ! current_user_can( 'manage_options' ) ) {

        $current_size = $file['size'];
        $current_size = $current_size / 1024; //get size in KB

        if ( $current_size > $file_size_limit ) {
            $file['error'] = sprintf( __( 'ERROR: File size limit is %d KB.' ), $file_size_limit );
        }

    }

    return $file;

}
add_filter ( 'wp_handle_upload_prefilter', 'limit_upload_size', 10, 1 );

Hope it helps!

4
  • This did the trick, I just removed the excluded admins, so that nobody can upload more than 1 MB. Thanks :D
    – dingo_d
    May 31, 2016 at 9:52
  • Is have added Capiedge's code, however my error message reads: "Error #-200: HTTP Error." Is it possible to replace this message with your own custom one? not sure why the specified error message is not showing up....
    – Elektra
    Aug 9, 2019 at 4:14
  • Tip 1: put this at the end of /wp-admin/includes/file.php | Tip 2: works only with non-administrator users Apr 18 at 20:06
  • Is there a way to add a URL in the error message ? May 23 at 2:16
3

You can hook into upload_size_limit and set a maximum filesize:

// Change the upload size to 1MB
add_filter( 'upload_size_limit', 'wpse_228300_change_upload_size' ); 
function wpse_228300_change_upload_size()
{
    return 1000 * 1024;
}
2
  • Thanks for the suggestion, but Capiedge was first so I used his code.
    – dingo_d
    May 31, 2016 at 9:53
  • 1
    Also, that'd extend the limit to plugin files upload as well. And won't allow you to upload plugins greater than 1 MB in size.
    – Kumar
    Feb 19, 2017 at 17:52
1

For me this is works great !

  function my_max_image_size( $file ) {
  $size = $file['size'];
  $size = $size / 1024;
  $type = $file['type'];
  $is_image = strpos( $type, 'image' ) !== false;
  $limit = 750;
  $limit_output = '750kb';
  if ( $is_image && $size > $limit ) {
    $file['error'] = 'Image files must be smaller than ' . $limit_output;
  }
  return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'my_max_image_size' );
-3

1: Theme Functions File

There are cases where we have seen that just by adding the following code in the theme function’s file, you can increase the upload size:

@ini_set( 'upload_max_size' , '64M' );
@ini_set( 'post_max_size', '64M');
@ini_set( 'max_execution_time', '300' );

2. Create or Edit an existing PHP.INI file

In most cases if you are on a shared host, you will not see a php.ini file in your directory. If you do not see one, then create a file called php.ini and upload it in the root folder. In that file add the following code:

upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300

3. htaccess Method

Some people have tried using the htaccess method where by modifying the .htaccess file in the root directory, you can increase the maximum upload size in WordPress. Open or create the .htaccess file in the root folder and add the following code:

php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
php_value max_input_time 300
2
  • Thanks for the contribution, but @ini_set usually never works (I think because of the security reasons), and I don't want to touch php.ini. The htaccess method seems odd, but still I got the answer above that works :)
    – dingo_d
    May 31, 2016 at 11:00
  • 3
    -1 because you didn´t read the question and blatantly copied from here without telling that: wpbeginner.com/wp-tutorials/…
    – flomei
    May 31, 2016 at 13:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.