Ever want to make a buddypress function that creates a group? Here’s how:
You can do this without writing any SQL. Buddypress uses a class called BP_Groups_Group to handle its group creation. BP_Groups_Group is defined in /groups/bp-groups-classes if you want to see it. I highly recommend taking a look at this, as you will discover other class methods that do other group-related things.
Say you want a plugin that upon activation creates a certain group. First add an action to the activation hook:
register_activation_hook(__file__, 'create_a_group');
Next, we need to call a function that instantiates the class BP_Groups_Group. Lets do this:
-
function create_a_group() {
-
-
$new_group = BP_Groups_Group;
-
-
$new_group->creator_id = 1;
-
$new_group->name = ‘test’;
-
$new_group->slug = ‘test’;
-
$new_group->description = ‘nothing’;
-
$new_group->news = ‘whatever’;
-
$new_group->status = ‘public’;
-
$new_group->is_invitation_only = 1;
-
$new_group->enable_wire = 1;
-
$new_group->enable_forum = 1;
-
$new_group->enable_photos = 1;
-
$new_group->photos_admin_only = 1;
-
$new_group->date_created = current_time(‘mysql’);
-
$new_group->total_member_count = 1;
-
$new_group->avatar_thumb = ‘some kind of path’;
-
$new_group->avatar_full = ‘some kind of path’;
-
-
$new_group -> save(); //this does the database insert
-
-
}
The whole point of doing it this way is so you that can use the save() method. Check this function out on line 90 of bp-groups-classes.php. It does all the SQL for you, and to reproduce this would be very tedious.
create_a_group() will dump a row into wp_bp_groups. But if you go to your site and look at the groups directory, it won’t show up. Why? Because Buddypress checks for certain kinds of metadata before it displays a group in the main directory. So add this:
-
function create_a_group() {
-
-
$new_group = new BP_Groups_Group;
-
-
$new_group->creator_id = 1;
-
$new_group->name = ‘test’;
-
$new_group->slug = ‘test’;
-
$new_group->description = ‘nothing’;
-
$new_group->news = ‘whatever’;
-
$new_group->status = ‘public’;
-
$new_group->is_invitation_only = 1;
-
$new_group->enable_wire = 1;
-
$new_group->enable_forum = 1;
-
$new_group->enable_photos = 1;
-
$new_group->photos_admin_only = 1;
-
$new_group->date_created = current_time(‘mysql’);
-
$new_group->total_member_count = 1;
-
$new_group->avatar_thumb = ‘some kind of path’;
-
$new_group->avatar_full = ‘some kind of path’;
-
-
$new_group -> save();
-
-
groups_update_groupmeta( $id, ‘total_member_count’, 1 );
-
groups_update_groupmeta( $id, ‘theme’, ‘buddypress’ );
-
groups_update_groupmeta( $id, ‘stylesheet’, ‘buddypress’ );
-
-
}
This is how Buddypress updates group metadata. Your new group should show up.
It is worth mentioning that the other database operations in Buddypress work exactly the same way: first you instantiate a database class, populate the object with data, then you call save(). But look at the code, there are tons of other methods for these kinds of classes that will help you do things without writing redundant SQL.
Basically, what you are doing here mirrors what Buddypress does in step one (case 1) of groups_create_group() on line 1451 of bp-groups.php. The reason you can’t just use this function is that you need your own data, not the canned query that comes with the plain old populate().
Thanks for this. I am just now diving into group in buddypress and I think this will be very helpful. I don’t know mysql so I imagine this will soon be very necessary for me.
Hi Dan,
I have an interesting project I’m working on and I would like to know if you have any suggestions.
I’m currently trying to use buddypress as a social network centered on a bible study with mutlitple lessons. The lessons are available online and I want to show which lessons each buddypress member has learned and invite them to learn newer lessons.
My idea is to use groups as the “lessons”. But what I would like to know is if I can add custom fields (for pulling content) to the creation of a group. I would want to include a field for the featured image, and the video to be embedded for that lesson.
My other idea was to create a custom page template. Here is a prototype I built before installing buddypress, http://newlifelb.com/daniel . It is not really using the buddypress members or groups, but I am researching to see if this could be done using buddypress.
What do you think?
Hey Shaun,
The groups architecture may work as a framework for lessons. But beware that it may be difficult to organize these lessons/groups into the views that you want. Basically BP gives you the all groups directory and that’s about it. Other views can be created, but require programming. Another pitfall I foresee will be differentiating lessons from other kinds of groups, in the directory and on users’ profiles. The groups metadata table may help you here.
You should check out BPDEV groups extra (http://bp-dev.org/plugins/). It is incompatible with 1.2 I believe, but may be easy to fix. See my post for at least one thing that needs to be changed. This plugin lets you configure extra fields on the group profile page. You can make one be a youtube embed or something like that. It is a pain to set up – you have to edit a config.php file in order to setup the fields you want.
Really a cleaner way to add group fields is to use the groups extension api. It requires that you know PHP, and possibly some MySQL for saving the data, although you can use
groups_update_meta()if you’re only adding one or two fields.Check out Group Documents. Also, Community Blogs may help you create a lesson that incorporates a linear set of teachings.
Good luck with your site!
– Dan
Wow, thanks Dan. I’m looking forward to trying out the groups extension API. I’ve never written a plugin before so I read up on how plugins work and for the past few days I’ve been going through your plugin code. It’s been really helpful.
lol, yeah thats just a typo and I changed it. The correct class is BP_Groups_Group. Thanks for telling me about this.
Is there a plugin that does above ?
My need is as stated below.
1) During user registration user enters a profile field, one of the text box field is city name.
2) Upon new user creation check if City (group) exists already. If YES assign the user to group (City)
3) If not create a new group (city) and assign the user.
I think the added_usermeta action would let you accomplish this. Have you written plugins before? If you have or are willing to learn, look up how to use the added_usermeta action. Within your function for this action, you can use the value of the city metadata field to query the groups and see if it exists and then add the group and/or add the user to the group.