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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | add_action('user_register', 'greenbox_add_user_to_new_buddypress_group'); //this should only fire on create user, therefore no problem with dupe groups function greenbox_add_user_to_new_buddypress_group($user_id) { $id_group = greenbox_create_bp_group($user_id); greenbox_add_user_to_group($user_id, $id_group); } function greenbox_create_bp_group($user_id) { $info = get_userdata($user_id); $group_args = array(); $group_args['name'] = "" . $info->user_nicename . "_" . $user_id; $group_args['description'] = "A group to hold the files of " . $info->user_nicename; $group_args['creator_id'] = 1; $group_args['status'] = 'private'; // could be hidden or public $id_group = groups_create_group($group_args); return $id_group; } function greenbox_add_user_to_group($user_id, $group_id) { if (!$user_id) return false; groups_accept_invite($user_id, $group_id); } |
So there you go, adding users to groups in Buddypress.
Disclaimer: All content on this site, is use at your own risk (Always backup before changing anything in your software/database/servers etc). Techs change, go out of date etc...I/we accept no liability if anything you use on this site adversely affects you.
Recent Comments