Prevent Duplicate BuddyPress Group Names
Do you want to stop duplicate BuddyPress group names from being used on your site? You are in luck as one of our members asked me the same.
Why do we need to prevent duplicate BuddyPress Group names?
No body wants to live in a city where every house has same number and looks similar from outside. Having duplicate group names is confusing.
How do we prevent duplicate BuddyPress Group names:-
Good question. We need to add a check at all the places from where the group names are updated.
How the BuddyPress Group Name is updated?
In normal case, there are two ways.
- The user can enter group name on the group create screen.
- Or the group admins can update it on group edit screen
We will see strategies to put a check for both these screens.
Before we go further and write code, we can break this duplicate prevention problem into two parts.
- Part 1:- Check if the given name is a duplicate BuddyPress group name?
- Part 2:- Prevent the duplicate name from being used on various updatable screens.
Part 1:- Check if the given group name is already used on the site.
We can write a simple function that tests if the given name exists. The second parameter is used when testing the duplicate name for existing groups.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Check if the given BuddyPress group name is duplicate. A group with same name already exists. * * @param string $name name to be checked. * @param int $group_id group id(exclude this group from test). * * @return bool */ function buddydev_is_duplicate_group_name( $name, $group_id = 0 ) { global $wpdb; $bp = buddypress(); $table = $bp->groups->table_name; $sql = $wpdb->prepare( "SELECT id FROM {$table} WHERE name = %s", $name ); // except this group, used for updating. if ( $group_id ) { $sql .= $wpdb->prepare( " AND id != %d", $group_id ); } return $wpdb->get_var( $sql ); } |
Part 2:- Preventing duplicates from being saved.
Since we have to do it for 2 screens, we will be breaking into two steps.
Step 1:- Prevent duplicate group names on group create screen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /** * On BuddyPress Group Creation, Prevent duplicate group name. */ function buddydev_check_duplicate_name_on_bp_group_create() { // If we're not at domain.org/groups/create/ then return false. if ( ! bp_is_groups_component() || ! bp_is_current_action( 'create' ) ) { return; } // If no current step is set, reset everything so we can start a fresh group creation. buddypress()->groups->current_create_step = bp_action_variable( 1 ); if ( 'group-details' != bp_get_groups_current_create_step() ) { return; } if ( empty( $_POST['group-name'] ) ) { return; } // duplicate. if ( buddydev_is_duplicate_group_name( $_POST['group-name'] ) ) { bp_core_add_message( __( 'Please use a different name.' ), 'error' ); bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) ); } } add_action( 'bp_actions', 'buddydev_check_duplicate_name_on_bp_group_create', 9 ); |
Step 2:- Disallow duplicate group names on group edits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * On BuddyPress Group Creation, Prevent duplicate group name. */ function buddydev_check_duplicate_name_on_bp_group_update() { if ( ! function_exists( 'bp_get_group_current_admin_tab' ) || 'edit-details' != bp_get_group_current_admin_tab() ) { return; } if ( empty( $_POST['group-name'] ) ) { return; } // duplicate. if ( buddydev_is_duplicate_group_name( $_POST['group-name'], $_POST['group-id'] ) ) { bp_core_add_message( __( 'Please use a different name.' ), 'error' ); bp_core_redirect( bp_get_group_permalink( groups_get_current_group() ) . 'admin/edit-details/' ); } } add_action( 'bp_screens', 'buddydev_check_duplicate_name_on_bp_group_update', 9 ); |
The code should be put in your bp-custom.php. Not sure how to add the code to your bp-custom.php? you can use our custom code service and one of our developers can put the things in place.
Happy BuddyPressing Buddy 🙂