Automatically Add New Users to a BuddyPress Group

Ever wished you could automatically add newly-registered users to a specific BuddyPress Group? I’ve had a number of clients make this request, and it turns out to be easier than I’d initially expected. Simply paste the following code into your theme’s functions.php file, replace <# group ID #> on line five with the numeric ID of the desired group, and any users who join after that will automatically become members of the group. I’ve tested this snippet with BuddyPress 1.2.x.

//Automatically add new users to a group
function automatic_group_membership( $user_id ) {
 if( !$user_id ) return false;

 groups_accept_invite( $user_id, <# group ID #> );
add_action( 'bp_core_activated_user', 'automatic_group_membership' );