Buddypress how to create a group programmatically in PHP (and add users)

In this post I’ll show you how to create a new group in PHP and then add users to the group. In this example I’m adding new users to a new group when they register.

  add_action('user_register', 'greenbox_add_user_to_new_buddypress_group'); //this should only fire on create user, therefore no problem with dupe groups

  function greenbox_add_user_to_new_buddypress_group($user_id) {


  $id_group = greenbox_create_bp_group($user_id);

  greenbox_add_user_to_group($user_id, $id_group);
  }

  function greenbox_create_bp_group($user_id) {

  $info = get_userdata($user_id);

  $group_args = array();

  $group_args['name'] = "" . $info->user_nicename . "_" . $user_id;

  $group_args['description'] = "A group to hold the files of " . $info->user_nicename;

  $group_args['creator_id'] = 1;

  $group_args['status'] = 'private';   //  could be hidden or public

  $id_group = groups_create_group($group_args);

  return $id_group;
  }

  function greenbox_add_user_to_group($user_id, $group_id) {

  if (!$user_id)
  return false;

  groups_accept_invite($user_id, $group_id);
  }

So there you go, adding users to groups in Buddypress.

11 thoughts on “Buddypress how to create a group programmatically in PHP (and add users)”

  1. Hi

    This has been a lifesaver honestly, iv spent the past 3 days trying to figure this out, youre brilliant!

    I can now create buddypress groups automatically once a user registers.

    I have 2 requests, to see if you can help just to finish it off:

    1) How do i set the $group_args[‘name’] = to be an input field from the registration submission form? I have tried setting the input field name yet it doesnt work!

    2) I want to restrict this to run only with instructors / anyone who fills in this field of the form, is this possible?

    3) I want them to be part of the group but not the administrator, the current creator id is set to 1, should i just modify that to the main id?

    Reply
    • hi Slastik,

      Glad you found it useful. Have a look at this plugin https://wordpress.org/plugins/buddypress-registration-groups-1/

      Its doing pretty close to what you want ( it shows groups on the buddypress register page ), and allows a new user to signup to those groups.

      If you want to allow a user to create a group on signup you just need to hack it abit. Pay particular attention to what the plugin is doing to save the group and assign the user to.

      Especially the call backs ( actions ):

      bp_core_signup_user
      bp_core_activated_user

      good luck 🙂

      Reply
  2. Hi mate

    Thanks for the reply honestly, iv spent the past day looking at both trying to populate the
    $group_args [`name`] =

    With buddypress xprofile field information.

    For some reason it just cant seem to pull the data, and so although the user does register, although yet to activate, no group is created as the field is empty – this is fine when i leave your $info code in there.

    Iv looked at xprofile get data with field as well as bp get data. None work.

    Any advice as to where else to look?

    Reply
    • Can someone help me out?
      1) When a teacher registers and inputs a new Institute in the form field, this should auto-create a Buddypress Group, and use the input text as the name of the Group
      2) The teacher would automatically be placed in the Group
      3) Students cant create groups
      4) The group will only be created if it doesnt exist and is unique, if it exists, the teacher will be told to choose from the dropdown selection

      Reply
  3. if the user is saved try this function:

    $the_thing_you_want = bp_get_member_profile_data( ‘field=Short Bio’ )

    sub in the name of the xprofile field.

    Reply
  4. works well !!!! i am trying to find a way to see if group already exists… if so just add user, if not create group and add user… in my case i use the users geolocation eg group name = $countryname.’-‘.$countrycode…. but not working

    $table_name = buddypress()->groups->table_name;

    $group_id = $wpdb->get_var( $wpdb->prepare( “SELECT id FROM {$table_name} WHERE name = %s”, strtolower($groupname) ));

    Reply

Leave a Comment