Call   0208 3131682   Mon-Fri 9am-6pm

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

By November 12, 2015 PHP

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.

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

9 Comments

  • slastik says:

    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?

    • louie171 says:

      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 🙂

  • slastik says:

    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?

    • Vinamra says:

      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

  • louie171 says:

    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.

  • Hi
    Is this code still working with the actual Buddypress?? and where do I insert it?

    Thanks

  • Greg Marriott says:

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

Leave a Reply