Prohibit create groups with existing name
-
Hi there everyone!
Is it possible to prohibit creating groups with existing name?
-
Yes. You can use the
groups_group_before_save
hook to check if the name already exists. If so, set the name to and empty string. This will result in theBP_Groups_Group::save()
method returningfalse
, and no group being created. Note, you’ll probably also want to give the user some kind of useful message to tell them what’s happened.Thanks a lot for Your advice!
Is it possible to do the same with Events? For example in Events Manager plugin?
This is what i was looking for.. thanks.. so should we use
bp_core_add_message
function to show the user an error?
Thanks,
ParagIs it possible to do the same with Events? For example in Events Manager plugin?
I’m not familiar with the plugin but likely you can do the same sort of thing. Drop the plugin author the same question on their support forum. Hopefully they’ll get back to you with the info.
so should we use
bp_core_add_message
function to show the user an error?
Yes, the
bp_core_add_message()
function will display a message you provide after a page reload. Be sure you havedo_action( 'template_notices' );
in your template because the function will use that hook.Also should i be using the search_groups method for searching for an existing group with the same name or is there a different way?
Also should i be using the search_groups method for searching for an existing group with the same name or is there a different way?
BP_Groups_Group::search_groups()
will get you a list of groups that match the first argument you pass to it. But the snag is, name and description matches are returned. We only want name matches.An alternative would be to use
BP_Groups_Group::group_exists( $slug )
. This method will return either the ID of the group if one is found, elsenull
. Keep in mind, the custom function you have hooked togroups_group_before_save
will be passed the value of$slug
.Thanks a lot Henry.. i tried that i am sure i am doing something wrong.. this is what i have and for some reason nothing happens… What am i doing wrong?
add_action('groups_group_before_save','RECN_CUSTOM_CHECK_DUPLICATE_NAME'); function RECN_CUSTOM_CHECK_DUPLICATE_NAME($fogroup) { if ( class_exists( 'BP_Groups_Group' ) ) : // $group_id = BP_Groups_Group::group_exists( $fogroup->slug ); if(!empty($group_id)) { bp_core_add_message( 'A group with the same name already exists. Please search for the existing group to send a membership request.', 'error' ); $fogroup->name=null; return $fogroup; } endif; return $fogroup; }
I had some time this morning so wrote this: http://pastebin.com/7wBvs7MM
Note, I’m using a custom database query because I couldn’t find anything already available in the API.
@henrywright
snippet works great ! IMO, this should be in core. Ask for enhancement please ! 🙂@danbp Thanks for testing! I don’t think everyone would want this in core because some websites need groups to have the same name. I could package it up as a plugin if there’s much interest in it?
+1 for a group option plugin.
I agree it should be core, with a tick box in settings to enable the feature or not.
+1 for the plugin option though.
I’d like some case testing as well to make sure “group1” can’t be created where “Group1” already exists.
If Group1 already exists, you can’t create a new group with the name Group1 or group1.
Hope that helps!
Great thanks
This works thanks..a lot @henrywright
Hi,
I have hit a problem with this in that I am not getting a user error message when trying to create a duplicate group – although it prohibits the new group creatation – it’s just that the user remains on the first stage without any error notification.
However, I think this is because we have renamed group(s) to fandom(s) instead
What parts of your code do I need to rename from groups to fandoms to make it work 1000% correctly?
Many thanks in advance
- The topic ‘Prohibit create groups with existing name’ is closed to new replies.