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.