Skip to:
Content
Pages
Categories
Search
Top
Bottom

Prohibit create groups with existing name

Viewing 16 replies - 1 through 16 (of 16 total)

  • Henry Wright
    Moderator

    @henrywright

    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 the BP_Groups_Group::save() method returning false, 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.


    Max Zhubr
    Participant

    @max-zhubr

    Thanks a lot for Your advice!

    Is it possible to do the same with Events? For example in Events Manager plugin?


    paragbhagwat
    Participant

    @paragbhagwat

    This is what i was looking for.. thanks.. so should we use

    bp_core_add_message

    function to show the user an error?

    Thanks,
    Parag


    Henry Wright
    Moderator

    @henrywright

    Is 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 have do_action( 'template_notices' ); in your template because the function will use that hook.


    paragbhagwat
    Participant

    @paragbhagwat

    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?


    Henry Wright
    Moderator

    @henrywright

    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, else null. Keep in mind, the custom function you have hooked to groups_group_before_save will be passed the value of $slug.


    paragbhagwat
    Participant

    @paragbhagwat

    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;
    }

    Henry Wright
    Moderator

    @henrywright

    @paragbhagwat

    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.


    danbp
    Moderator

    @danbp

    @henrywright
    snippet works great ! IMO, this should be in core. Ask for enhancement please ! 🙂


    Henry Wright
    Moderator

    @henrywright

    @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?


    danbp
    Moderator

    @danbp

    +1 for a group option plugin.


    Venutius
    Moderator

    @venutius

    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.


    Henry Wright
    Moderator

    @henrywright

    @venutius

    If Group1 already exists, you can’t create a new group with the name Group1 or group1.

    Hope that helps!


    Venutius
    Moderator

    @venutius

    Great thanks


    paragbhagwat
    Participant

    @paragbhagwat

    This works thanks..a lot @henrywright


    onlinebusinesssupport
    Participant

    @onlinebusinesssupport

    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

Viewing 16 replies - 1 through 16 (of 16 total)
  • The topic ‘Prohibit create groups with existing name’ is closed to new replies.
Skip to toolbar