Let’s say that you that you are building a custom application with BuddyPress (like maybe this) and you had a need to programmatically create a BuddyPress group. Now there are ways to customize the BuddyPress group creation process that I will cover in a future post, but there is actually a very simple function that will do all the heavy lifting. So, without any further ado, I’d like to introduce you to groups_create_group().
Defined on line 83 of bp-groups-functions.php, groups_create_group() does exactly what you would think that it would; it creates a new BuddyPress group with the provided parameters and returns the ID of the newly created group. Let’s take a look at the default arguments for this function and then step through each.
85 86 87 88 89 90 91 92 93 94
$defaults = array( 'group_id' => 0, 'creator_id' => 0, 'name' => '', 'description' => '', 'slug' => '', 'status' => 'public', 'enable_forum' => 0, 'date_created' => bp_core_current_time() );
Use this parameter to define a group to update. Yes that’s right, you can use groups_create_group() to update an existing group.
Default: 0, create a new group.
The id of the user to be used as the group creator.
Default: 0, uses the currently logged in user.
The name of the new group to be created.
A description for the new group.
The unique slug to be used for this group.
Default: ”, will auto calculate based off of the group name
The visibility of the group. Valid options are ‘public’, ‘private’, or ‘hidden’ as defined on line 342 of bp-groups-loader.php.
Whether or not this group supports a forum. Use 1 for yes and 0 for no. Note: this will not create a forum for the group. In order to do that, use groups_new_group_forum()
The date to log as the group creation date.
I hope this has been helpful to you! If you have any questions or corrections, please leave a comment below.
2 thoughts on “Create Groups in BuddyPress Programmatically”
Any idea if this still works? I’ve read that the code has changed in the last 4 years. What I’m trying to do (hypothetically) is enable people to fill out a contact form which then automatically generates a group and sends the members an invitation via email (the emails would be submitted on a contact form). Or if you do custom code projects like this, may be interested in talking further.
Hi Jenny! Yes, this method should still work just fine.