BuddyPress – Limit creation of groups (private only) – WITH CODE
-
Hello guys!
I’ve seen this question in here a couple of times and maybe others would enjoy this code.
I couldn’t find a good solution in trying to “hide” the official and hidden groups, when users are creating them (it just would not allow me to hide them!).I then managed to fix a code and make it work so at least it shows an error when a user (besides admin) tries to create a group that is NOT private. In this way when a user tries to create a public/hidden group it will not allow the user to “proceed” with the next step unless private group is chosen.
Feel free to update the CSS to your liking!
Hope it helps, enjoy! 🙂
function restrict_group_type_selection() { // Ensure this script only runs during the group settings creation step if (bp_is_group_creation_step('group-settings')) { // Check if the user is not an admin (wrap the condition outside the script for cleaner HTML output) if (!current_user_can('manage_options')) { ?> <style type="text/css"> .group-type-error-message { color: #ffffff; background-color: #f95959; position: relative; margin-top: 20px !important; border: 0 !important; font-weight: 600; } } </style> <script type="text/javascript"> document.addEventListener('DOMContentLoaded', function () { const nextButton = document.getElementById('group-creation-next'); // Target the "Next step" button if (nextButton) { nextButton.addEventListener('click', function (event) { const selectedGroupType = document.querySelector('input[name="group-status"]:checked'); let errorContainer = document.getElementById('group-type-error-message'); // Remove any existing error message if (errorContainer) { errorContainer.remove(); } if (!selectedGroupType || selectedGroupType.value !== 'private') { // Prevent moving to the next step if the group type is not 'private' event.preventDefault(); // Create and display a styled error message errorContainer = document.createElement('div'); errorContainer.id = 'group-type-error-message'; errorContainer.className = 'group-type-error-message'; errorContainer.innerText = 'You are only allowed to create private groups.'; // Insert the error message after the "Next" button nextButton.parentNode.appendChild(errorContainer); } }); } }); </script> <?php } } } add_action('bp_after_group_settings_creation_step', 'restrict_group_type_selection');
- You must be logged in to reply to this topic.