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:

  1. function create_a_group() {
  2.  
  3.     $new_group = BP_Groups_Group;
  4.  
  5.         $new_group->creator_id = 1;
  6.         $new_group->name = ‘test’;
  7.         $new_group->slug = ‘test’;
  8.         $new_group->description = ‘nothing’;
  9.         $new_group->news = ‘whatever’;
  10.         $new_group->status = ‘public’;
  11.         $new_group->is_invitation_only = 1;
  12.         $new_group->enable_wire = 1;
  13.         $new_group->enable_forum = 1;
  14.         $new_group->enable_photos = 1;
  15.         $new_group->photos_admin_only = 1;
  16.         $new_group->date_created = current_time(‘mysql’);
  17.         $new_group->total_member_count = 1;
  18.         $new_group->avatar_thumb = ‘some kind of path’;
  19.         $new_group->avatar_full = ‘some kind of path’;
  20.  
  21.     $new_group -> save(); //this does the database insert
  22.  
  23. }

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:

  1. function create_a_group() {
  2.  
  3.     $new_group = new BP_Groups_Group;
  4.  
  5.         $new_group->creator_id = 1;
  6.         $new_group->name = ‘test’;
  7.         $new_group->slug = ‘test’;
  8.         $new_group->description = ‘nothing’;
  9.         $new_group->news = ‘whatever’;
  10.         $new_group->status = ‘public’;
  11.         $new_group->is_invitation_only = 1;
  12.         $new_group->enable_wire = 1;
  13.         $new_group->enable_forum = 1;
  14.         $new_group->enable_photos = 1;
  15.         $new_group->photos_admin_only = 1;
  16.         $new_group->date_created = current_time(‘mysql’);
  17.         $new_group->total_member_count = 1;
  18.         $new_group->avatar_thumb = ‘some kind of path’;
  19.         $new_group->avatar_full = ‘some kind of path’;
  20.  
  21.     $new_group -> save();
  22.  
  23.     groups_update_groupmeta( $id, ‘total_member_count’, 1 );
  24.     groups_update_groupmeta( $id, ‘last_activity’, time() );
  25.     groups_update_groupmeta( $id, ‘theme’, ‘buddypress’ );
  26.     groups_update_groupmeta( $id, ‘stylesheet’, ‘buddypress’ );
  27.  
  28. }

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

19 Comments

    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.

  • Hi, Dan. This is exactly what I’m looking for, but I’m stumped by a change you made between your first draft and the second one. Why did line 4 start out as

    $new_group = BP_Groups_Group;

    but then change to

    $new_group = new custom_group;

    That gives me: Fatal error: Class ‘custom_group’ not found in …

    Thanks,
    Lee Grey

  • lol, yeah thats just a typo and I changed it. The correct class is BP_Groups_Group. Thanks for telling me about this.

  • All my friends…

    you can also use this very small code to create a bp group

    function create_a_group() {
    global $bp;

    $bp->groups->new_group_id = groups_create_group( array( ‘name’ => ‘TEST GROUP’, ‘description’ => ‘TEST GROUP DESC’, ‘slug’ => groups_check_slug( sanitize_title( esc_attr( ‘TEST GROUP’ ) ) ), ‘date_created’ => gmdate( “Y-m-d H:i:s” ), ‘status’ => ‘public’ ) );

    groups_update_groupmeta( $bp->groups->new_group_id, ‘total_member_count’, 1 );
    groups_update_groupmeta( $bp->groups->new_group_id, ‘last_activity’, gmdate( “Y-m-d H:i:s” ) );
    }

    /* Just paste the above code in your theme’s functions.php file and call this function wherever you need….. */

  • 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.

  • Hi there

    thank you very much for your explanations. It almost works for me – it inserts the group into wp_bp_groups, but it does not show them, because – as you point out – they will not show unless there’s corresponding wp_bp_groups_groupsmeta – data.

    Now on your line:

    “groups_update_groupmeta( $id, ‘total_member_count’, 1 );”

    where is the “$id” coming from? If I replace that with an existing group-id, it works.

    I’d appreciate any feedbacks…
    Raphael

  • …very sorry, I just found it out:

    It has to be:

    groups_update_groupmeta( $new_group->id, ‘total_member_count’, 1 );
    :)

    Hope it’s of help for others too :)

  • Hello mml,

    that sounds like just what I might need! Did you succeed in getting this to work? Do you mind sharing the code/steps to get it to work. Or did you even make a plugin?
    Many thanks!

  • I was looking all over for this info. I have to thank you for posting.
    I dug all over the buddypress codex and could not find such details for the create functions.

  • Hey I tried the above code to Create a Grp from Function…The pblm is even after adding the metadata Grp is not visible. The Grp count is getting increased but the grp is not getting displayed

  • how and where to call this function…Can u please tell me the brief code for that.

  • This is a really helpful article, thanks for posting it!

    Has anyone been able to incorporate this into a shortcode or better yet an add-on plugin that will allow a command to create a new group each time a new post is made?

    I envision having a separate BP group forum for each post.

  • thanks for the tutorial!
    for some reason I do not get new group created.

    Do I have to add this code to functions.php of my theme?
    Is there any way to get this code working to create multiple groups at a time?

    thank you!

  • Hi Dan,

    Using Buddypress 1.5.5, I found that the groups would not display unless invite_status meta data was added:

    groups_update_groupmeta( $id, ‘invite_status’, ‘members’ );

  • Hello Admin,

    I want to create event and fanpage on my site same as created on facebook.
    Can u suggest me any plugin??
    or any other way.. ?

Leave a Reply