Support » Plugin: Upload Quota per User » Limit Number of Uploads Based on Role?

  • Resolved truelux

    (@truelux)


    The quota works great but is there also a way to limit the number of uploads a user can make? So limit them to 5… then when they delete 1, they can then upload another.

    I found this code online but the second half of it doesn’t work:

    add_filter( 'wp_handle_upload_prefilter', 'limit_uploads_for_user_roles' );
    function limit_uploads_for_user_roles( $file ) {
      $user = wp_get_current_user();
      $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', 7, $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;
    }
    
    add_action('delete_attachment', 'decrease_limit_uploads_for_user');
    
    function decrease_limit_uploads_for_user( $id ) {
       $user = wp_get_current_user();
    
       $limit_roles = array('contributor');
       $filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles, $user );
       if ( array_intersect( $limit_roles, $user->roles ) ) {
         $post = get_post( $id);
         if ( $post->post_author != $user->ID ) return;
         $count = get_user_meta( $user->ID, 'upload_count', true ) ? : 0;
         if ( $count ) update_user_meta( $user->ID, 'upload_count', $count - 1 );
       }
    }

    https://wordpress.org/plugins/upload-quota-per-user/

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Limit Number of Uploads Based on Role?’ is closed to new replies.