Last active
December 29, 2025 05:51
-
-
Save annuman97/20221c9c2cde2c28f3fb1fc7805c0e28 to your computer and use it in GitHub Desktop.
Limit how many FluentCommunity Spaces a user can join. Applies to both Active and Pending memberships
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter('rest_pre_dispatch', function ($result, $server, $request) { | |
| // If another handler already returned something, respect it | |
| if (!empty($result)) { | |
| return $result; | |
| } | |
| // Target only Space join REST route | |
| // Route example: /fluent-community/v2/spaces/{slug}/join | |
| $route = $request->get_route(); | |
| if ( | |
| strpos($route, '/fluent-community/v2/spaces/') !== 0 || | |
| substr($route, -5) !== '/join' | |
| ) { | |
| return $result; | |
| } | |
| // Must be logged in | |
| $userId = get_current_user_id(); | |
| if (!$userId) { | |
| return $result; | |
| } | |
| // Set max allowed spaces (default: 3) | |
| $maxSpaces = (int) apply_filters( | |
| 'my_fcom_max_spaces_per_user', | |
| 3, | |
| $userId, | |
| $request | |
| ); | |
| // Make sure FluentCommunity is available | |
| if (!class_exists('\FluentCommunity\App\Models\SpaceUserPivot')) { | |
| return $result; | |
| } | |
| // Count how many spaces user already joined (active + pending) | |
| $joinedSpacesCount = \FluentCommunity\App\Models\SpaceUserPivot::where('user_id', $userId) | |
| ->whereIn('status', ['active', 'pending']) | |
| ->count(); | |
| // Block joining if limit reached | |
| if ($joinedSpacesCount >= $maxSpaces) { | |
| return new \WP_Error( | |
| 'fcom_space_limit_reached', | |
| sprintf( | |
| 'You have already joined the maximum allowed number of Spaces (%d).', | |
| $maxSpaces | |
| ), | |
| [ | |
| 'status' => 403 | |
| ] | |
| ); | |
| } | |
| return $result; | |
| }, 10, 3); | |
| add_filter('my_fcom_max_spaces_per_user', function ($limit, $userId) { | |
| return 5; // Give the number of spaces user can join | |
| }, 10, 2); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment