Force BuddyPress to allow unique group names only

Jotted by brajesh on July 3, 2010

As you may be knowing, BuddyPress does not forces users to create unique group names. If you are the webmaster of a social network, you may not want to allow same group name for multiple groups as it will cause confusions among the users. So, here is a small code snippet to force buddypress to allow creation of unique groups only.

add_filter("groups_group_name_before_save","allow_unique_names_only");
function allow_unique_names_only($name){
    global $bp;
 
  $groups=BP_Groups_Group::search_groups($name);//search groups
  $duplicate=false;
  if('group-details'==$bp->groups->current_create_step&&!empty($groups)&&$groups['total']>0){
      //we have match, but is there an exact match
      foreach($groups['groups'] as $g){
         $group=new BP_Groups_Group($g->group_id);
 
         if($group->name==$name){
              $duplicate=true;//found duplicate
              break;//break the loop;
          }
      }
if($duplicate){
        bp_core_add_message("A group with this name already exists. Please Choose another name.","error");
        bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $bp->groups->current_create_step . '/' );
  }
 
}
 return $name;
}

Put the above code in your bp-custom.php and you are good to go. Go ahead and test by creating groups with same name. It will inform you that a group with same exists, please use another name.

Credit: Thanks goes to “gwu” for asking this question in buddydev forum.

I am looking forward to your comments.

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]
If you enjoyed this post, make sure you subscribe to my RSS feed!
Grab my other free plugins here at wordpress Plugins repository.
We will keep coming with more informative tips,tricks,tutorials to keep you updated with wordpress/buddypress/wordpress mu regularly.If you want to help us,You can ,by spreading the words.We provide free and premium plugins/support for wordpress Mu,buddypress at a very low cost (starting with $30 for 3 months membership).If you or anyone you know,needs some real good plugins/themes for their wordpress Mu/buddypress powered site, Please refer them to http://BuddyDev.com.We will highly appreciate your support and keep coming with more useful and free stuffs.


One Response to “Force BuddyPress to allow unique group names only”

  1. Shane says:

    Neat little tool. Thanks for sharing it, it works well,

    Shane.


Leave a Reply


Get Adobe Flash playerPlugin by wpburn.com wordpress themes