On a Buddypress and BBPress enabled WordPress site, I wanted to set things up so that when a member joins a group, he/she will also automatically be subscribed to its forum. I couldn’t find a clear description of how to do this, so I thought I’d share what worked for me.
First, here’s the code. You can paste this into the functions.php file in your theme directory, or the bp-custom.php file in your plugins directory. I think it should work as is so long as you only have one forum per group.
// Add member who joins a group to its forum add_action( 'groups_join_group', 'auto_subscribe_forum', 10, 2 ); // function triggered when user joins group function auto_subscribe_forum ( $group_id, $user_id ) { // checks to see if there's a user ID if( !$user_id ) return false; // Get's an array of forum IDs for the group $forum_ids = bbp_get_group_forum_ids( $group_id ); // checks to see if there is a forum ID if ( !empty( $forum_ids ) ) { // gets the first value from the array, assuming there is one forum $forum_id = array_shift( $forum_ids ); } // subscribes user to forum bbp_add_user_forum_subscription( $user_id, $forum_id ); }
Basically, you’re using the groups_join_group
handle to trigger an action when someone joins a group (for example, by selecting the join group button/link on the group’s home page). The only slight oddity is that bbp_get_group_forum_ids
returns an array (I guess in case the group has more than one forum), so you need to convert this into a single number. I do this by using the array_shift function which returns the first value from the array.
Worth noting too that this would work with other handles too, for example when a user accepts a group invite (groups_accept_invite
). I’ve found Hookr to be a really useful site to find, well, hooks.
Hope that helps!