Support » Fixing WordPress » Limit the total space in every user

  • Resolved 凱寧

    (@kennyateam990)


    I use the buddypress + rtmedia plugins to create website.

    And I met a problem that how can I limit the total space in every user ?

    for example

    every user has 10MB to upload the video,photo,music in their profile.

    or

    every user can upload 10 photos in their profile.

    can that do it via theme`s functions.php ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello kennyateam990,

    Maybe try this code it might help.

    
    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;
    }
    

    Note: The count starts when you add the function and the filter: all previously uploaded files are not counted.

    The roles to limit can be altered also via filter.

    The limit can be changed via filter (default 10 in my code)

    As an alternative, you can check this plugin as well

    Hope this helps.

    Thanks.

    Thread Starter 凱寧

    (@kennyateam990)

    @kartiks16 yeah thanks the code works.

    but when I upload reach for 10 photos,they don`t have notice to display “Upload limit has been reached for this account!”

    Hey kennyateam990

    Happy to hear that code worked.

    Hope you have made edits in the code with your values and the error should appear when it exceeds the limit, so try to upload the 11th photo and check.

    Also, try out the plugin if it is easy for you.

    Thanks.

    Thread Starter 凱寧

    (@kennyateam990)

    yeah,I upload the 11th photo and check,but nothing happen.how come?

    ok,I will try this plugin after finish this code.

    Great kennyateam990!

    Feel free to ask if you have any further questions or you can mark it as closed/resolved.

    Thanks.

    Thread Starter 凱寧

    (@kennyateam990)

    $file['error'] = __('Upload limit has been reached for this account!', 'yourtxtdomain');

    I can`t understand the line ‘yourtxtdomain’ mean ?

    what should I text for ?

    It doesn’t really matter much, but you can replace that with your domain name.

    Even though it does not display, you need to debug little to see if you get a proper result.

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Limit the total space in every user’ is closed to new replies.