• I need to restrict the number and the size of uploaded images. WordPress site.

    There are quite a few articles about this (this forum and elsewhere), but none has worked so far. And I am not a pro.
    Some solutions:
    a) .htaccess:
    i) php_value upload_max_filesize XM (this worked fine, even with = XM.)
    Less than ideal, because it stops any upload, like updating plugins.

    ii) php_value max_file_uploads 50 – did not work.

    b) ini.php

    max_file_uploads 50 – did not work either.
    Less than ideal as well, because it stops any upload, like updating plugins.

    c) code: There are quite a few articles about this, but no one worked either. Once the limit is passed, no matter the # of uploads, you get the same error message. It seems that somehow the browser has the error or the variables are not being cleared. But I do not know.

    add_filter( 'wp_handle_upload_prefilter', 'limit_uploads_for_user_roles'
     );
    
     function limit_uploads_for_user_roles( $file ) {
       $user = wp_get_current_user();
       // add the role you want to limit in the array
       $limit_roles = array('contributor');
       $filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles,
     $user );
       if ( array_intersect( $limit_roles, $user->roles ) ) {
         $upload_count = get_user_meta( $user->ID, 'upload_count', true ) ? :
     0;
         $limit = apply_filters( 'limit_uploads_for_user_roles_limit', 10,
     $user,$upload_count, $file );
         if ( ( $upload_count + 1 ) > $limit ) {
           $file['error'] = __('Upload limit has been reached for this
     account!','yourtxtdomain');
         } else {
           update_user_meta( $user->ID, 'upload_count', $upload_count + 1 );
         }
       }
       return $file;
     }

    And another one:

    $size = $size / 1024; // Calculate down to KB
    
         // get imagetype of upload
         $type = $file['type'];
         $is_image = strpos($type, 'image');
    
         // set sizelimit
         $limit = 700; // Your Filesize in KB
    
         // set imagelimit
         $imagelimit = 7;
    
         // set allowed imagetype
         $imagetype = 'image/jpeg';
    
         // query how many images the current user already uploaded
         global $current_user;
         $args = array(
             'orderby'         => 'post_date',
             'order'           => 'DESC',
             'numberposts'     => -1,
             'post_type'       => 'attachment',
             'author'          => $current_user->ID,
         );
         $attachmentsbyuser = get_posts( $args );
    
         if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the
     image is small enough
             $file['error'] = 'Image files must be smaller than '.$limit.'KB';
         } elseif ( $type != $imagetype ) { // check if image type is allowed
             $file['error'] = 'Image must be ' . $imagetype . '.';
         } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if
     the user has exceeded the image limit
             $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded
     for this user.';
         }
         return $file;

    The problem with either one is that once it triggers the max upload and then you delete everything and start a fresh upload, you will always receive the same error message – max upload reached.
    It is not clearing the variables?

    The source for the codes is here.

    Thank you very much for any help.

    PS. Even when this pieces working, we can forsee another problem: once uploaded correctly, and user wants to delete images and start with new ones within the limit, we will probably bump onto another issue. But one problem per day… now into de first week trying to solve this. Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘limit number of image upload’ is closed to new replies.