Create Groups in BuddyPress Programmatically

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().

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.

/BuddyPress/blob/master/src/bp-groups/bp-groups-functions.php
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()
	);

group_id

Use this parameter to define a group to update. Yes that’s right, you can use groups_create_group() to update an existing group.

Required: no
Default: 0, create a new group.

creator_id

The id of the user to be used as the group creator.

Required: no
Default: 0, uses the currently logged in user.

name

The name of the new group to be created.

Required: yes
Default: ”

description

A description for the new group.

Required: no
Default: ”

slug

The unique slug to be used for this group.

Required: no
Default: ”, will auto calculate based off of the group name

status

The visibility of the group. Valid options are ‘public’, ‘private’, or ‘hidden’ as defined on line 342 of bp-groups-loader.php.

Required: no
Default: ‘public’

enable_forum

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

Required: no
Default: 0

date_created

The date to log as the group creation date.

Required: no
Default: bp_core_current_time()


I hope this has been helpful to you! If you have any questions or corrections, please leave a comment below.

Leave a Reply