Skip to content

Instantly share code, notes, and snippets.

@dcavins
Created September 20, 2017 17:08
Show Gist options
  • Select an option

  • Save dcavins/c7877bd0760c14a7740384b58e588ab3 to your computer and use it in GitHub Desktop.

Select an option

Save dcavins/c7877bd0760c14a7740384b58e588ab3 to your computer and use it in GitHub Desktop.
When a user joins a BuddyPress group, this action adds that user to all ancestor groups.
<?php
/* groups_join_group is called whenever someone joins a group. */
add_action('groups_join_group', 'group_join_add_ancestors', 10, 2);
function group_join_add_ancestors( $group_id, $user_id ) {
// I'm assuming that you'll want to join to all ancestors:
$ancestor_ids = hgbp_get_ancestor_group_ids( $group_id );
// To only include groups that the user can visit, use this version:
// $ancestor_ids = hgbp_get_ancestor_group_ids( $group_id, $user_id );
foreach( $ancestor_ids as $ancestor_id ) {
groups_join_group( $ancestor_id, $user_id );
}
/*
* Note that this will do some recursing because groups_join_group()
* will fire the action "groups_join_group" for each group joined.
* If we only joined to the parent group and relied on the
* "groups_join_group" action to fire the next step, the method could
* break if the user already belongs to one of the parent groups.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment