Extending BuddyPress Group Hierarchy – Join to Upstream Groups


A frequent feature request for BuddyPress Group Hierarchy has been to automatically join members of a subgroup to its parent groups. While pretty straightforward to implement, there are two main reasons this feature won’t make it into the plugin:

  1. This behavior isn’t appropriate for many or even most setups
  2. Everyone who has requested it has wanted it to work on groups by different criteria — some want groups of a certain depth, others a specific set of groups, others all the children of a certain group. Building a UI that would let this work the way anyone wants would overwhelm the project.

Instead, I decided to post this sample implementation, including a few ways to select groups. Take a look!

The Code

The code below can be included in your theme’s functions.php file, or can be part of a new plugin.

Note for the true novice: you must choose one of the if statements in the block below with this code and substitute the placeholders for things you care about. It will not work pasted as is.

/* groups_join_group is called whenever someone joins a group in both BP 1.2 and 1.5.x */
add_action('groups_join_group', 'my_join_group_action', 10, 2);
function my_join_group_action( $group_id, $user_id ) {
	/* This part is critical - you must select ONLY the groups you want to START this chain
	 * This function will recurse up to the toplevel on its own, so if you have a parent group
	 * in the list, you will waste resources trying to join the same group multiple times
	 * as this function is called on each step up the tree
	/** Pick from a list of groups */
	if( in_array( $group_id, ARRAY_OF_GROUPS_I_CARE_ABOUT ) ) {
	/* -- OR -- */
	/** Select groups by depth */
	if( count( bp_group_hierarchy_get_parents() ) == DEPTH_OF_GROUP_I_CARE_ABOUT_EG_3 ) {
		/* However you choose to select groups, once the group_id matched this part will 
		 * walk up the tree, joining the user to each parent group along the way
		$parents = bp_group_hierarchy_get_parents();
		foreach( $parents as $parent_group_id ) {
			groups_join_group( $parent_group_id, $user_id );

If this helps you, or if you have some code to select the groups that matter to YOU, feel free to post in the comments below!

6 Responses

  1. OC2PS says:

    Thanks for this.

    I am a true novice. Which file are we talking about here?

    Not sure I understand the reasons you have given for not including this in the plugin at least as an option where admin can switch it on or off. Please can you elaborate a bit?

    • ddean says:

      This code could go either in a new plugin, or in your theme's functions.php file. That's a great question! I'll update the post to include this.

      And I'm happy to elaborate. I guess what I failed to explain above is that this plugin is intended to allow BuddyPress groups to be subgroups. Over time, certain things have been added: an interface for viewing a group's subgroups, a tree for navigating all groups, etc. But all these features are about enabling subgroups , i.e. adapting the parts of BuddyPress that assume a flat group structure to accommodate subgroups.

      Adding features for membership or activity propagation starts us down the path of trying to enable anything anyone would want to do with subgroups. In addition to being practically impossible, this takes time and energy away from improving the core product.

      On top of that, what I was trying to describe in the post is that no feature is ever as simple as you think. Here's a quick example:
      You want "just" an option. But a global on/off switch for membership propagation means that anyone who can create a subgroup can circumvent the privacy settings of parent groups. This would harm sites that use a parent group as a "category," but who may want to use this function for child and grandchild groups. So, now we "just" also need a UI and code for stopping the process, or excluding groups from the process. Are all private groups excluded, or just top-level ones? Does it vary from install to install?…

      These kinds of issues, and the time needed to design and code and TEST these different scenarios, is why I won't just expand the scope of the plugin to include loosely-related features.

      • OC2PS says:

        I understand that you don't want to go down the slippery slope of feature creep.

        In the example you have used, however, I think we are getting muddled in a fringe case or a corner case. If the on/off option is available, and privacy is a major concern the way you have described, then surely the site owners can happily not switch membership propagation ON.

        I know I am biased and want to resist the temptation of equating what I want with what "most people" want. However, since in the beginning of the article you say that this is a frequent feature request, I can't help but think that I am not the only one who wants this, and that membership propagation is not itself a fringe case. In fact, to be completely honest, I consider membership propagation to be a core requirement for a hierarchical group structure.

        All that being said, you have been so kind to already write up the core code for membership propagation above. It might make sense if you wrote up a plugin that adds upwards/downwards propagation (if BP Group Hierarchy is installed)…if not add this option to the BP Group Hierarchy plugin itself….

  2. intrd says:

    David, thanks! this almost solved my problem..
    but i have a problem with new groups.. a user who joined at the site_root in the past does not join automatically at the new parent groups..
    I'm trying to make SiteRoot group to show all Parent Groups streams without force members to join at all subgroups, did you have any sugestion?

  3. Johnny says:

    Are you saying you can restrict which groups are available as parents?

    Can we do the top 3 in the chain?



  4. W.J. Kok says:

    Awsome! Thanks for sharing this piece.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>