Skip to content

Instantly share code, notes, and snippets.

@shanebp
Created March 29, 2016 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shanebp/72a0c6fe7ffb6f9c1424a30bdb455d4f to your computer and use it in GitHub Desktop.
Save shanebp/72a0c6fe7ffb6f9c1424a30bdb455d4f to your computer and use it in GitHub Desktop.
BuddyPress prevent duplicate Group Names
function pp_check_group_name( $group_new ) {
if ( 'group-details' == bp_get_groups_current_create_step() ) {
$args = array(
'per_page' => null,
'populate_extras' => false,
'update_meta_cache' => false
);
$groups = groups_get_groups( $args );
foreach ( $groups['groups'] as $group ) {
if( $group->name == $group_new->name ) {
$group_new->name = '';
pp_group_name_error_message();
break;
}
}
}
}
add_action( 'groups_group_before_save', 'pp_check_group_name' );
function pp_group_name_error_message() {
global $l10n;
$mo = new MO();
$mo->add_entry(
array(
'singular' => 'There was an error saving group details. Please try again.',
'translations' => array( __ ('That name is already being used. Please use a different name.', 'your-domain' ) )
)
);
if ( isset( $l10n['buddypress'] ) )
$mo->merge_with( $l10n['buddypress'] );
$l10n['buddypress'] = &$mo;
unset( $mo );
}
@valuser
Copy link

valuser commented Oct 18, 2016

Many thanks for this.
The introduction of group types, imho, changes a lot.
Groups with the same name but with different type classification is real life!
So if type was classified by location a bp install could have a solar-energy group in NY and a solar-energy group in SF.
(NY and SF being bp group types)
That would be normal and actually beneficial.

Perhaps what needs to be to be avoided now, is not so much groups with duplicate names as groups of the same bp group type with duplicate names.

@francyesco-epsilon
Copy link

francyesco-epsilon commented Sep 15, 2020

Not working if someone rename the group to some group name already existent, also keep attention is case sensitive.
EDIT: found "official" solution here https://buddydev.com/prevent-duplicate-buddypress-group-names/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment