If your buddypress site has the ability for all users to create groups, it’s always a good idea to ensure group names are original.
This code snippet ensure original names and inserts an error message to the user if the name has already been taken.
bp-custom.php
// Creating the markup
function 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 = '';
group_name_error_message();
break;
}
}
}
}
add_action('groups_group_before_save', 'check_group_name');
// translatable error message
function group_name_error_message()
{
global $l10n;
$mo = new MO();
$mo->add_entry(array(
'singular' => 'That group name is already in use. Please try again.',
'translations' => array(
__('That name is already being used. Please use a different name.', 'Buddypress')
)
));
if (isset($l10n['buddypress'])) $mo->merge_with($l10n['buddypress']);
$l10n['buddypress'] = & $mo;
unset($mo);
}
Hello, =D
I tested the code, he don’t work with me =/
Also, you did what Harry told ? 😀
Thanks a lot for your answer
Michaël
Hello,
The code works great. The only problem is that the name restriction is case sensitive, so it is still possible to have groups with essentially the same name, e.g. one called ‘Sports’ and another one called ‘sports’. Is it possible to add something to this code to make it non case sensitive?
Thanks in advance