Support » Plugin: BP Group Hierarchy » [Plugin: BP Group Hierarchy] group members automatically added to a subgroup

  • If someone is looking for adding group members from “parent” group to sub group automatically I did it by adding this code to /wp-content/plugins/bp-custom.php:

    // 3 functions exending plugin BP-group-hierarchy
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    
    if (is_plugin_active('bp-group-hierarchy/index.php')){
    function gh_add_users_when_group_created() { global $wpdb, $bp;
    
     $cur_gr_id = $bp->groups->current_group->id;
        $cur_gr_par_id = $wpdb->get_var($wpdb->prepare("SELECT DISTINCT g.parent_id FROM {$bp->groups->table_name} g WHERE g.id=%d", $cur_gr_id));
        if ($cur_gr_par_id) {
    
            $mysql = "SELECT DISTINCT user_id FROM wp_bp_groups_members WHERE group_id=$cur_gr_par_id";
    
            $results = $wpdb->get_results($mysql);
                    foreach ($results as $us) {
                           groups_accept_invite($us->user_id, $cur_gr_id);
            }
        }
    
    }
    add_action( 'bp_after_group_creation_step_buttons', 'gh_add_users_when_group_created',80 );
    
    function gh_add_user_when_user_is_added_to_parent_group($group_id, $user_id) {
        global $bp;
        $cur_gr_id = $bp->groups->current_group->id;
        $bphc = new BP_Groups_Hierarchy;
        $bphc->bp_groups_hierarchy($cur_gr_id);
        $chil = $bphc->has_children();
    
        if (empty($chil))
            return false;
        foreach ($chil as $child => $val) {
                groups_accept_invite($user_id, $val);
        }
    
    }
    add_action( 'groups_join_group', 'gh_add_user_when_user_is_added_to_parent_group', 10, 2);
    
    function gh_remove_user_when_is_deleted_in_parent_group($user_id, $group_id ) {
        global $bp;
        $cur_gr_id = $bp->groups->current_group->id;
        $bphc = new BP_Groups_Hierarchy;
        $bphc->bp_groups_hierarchy($cur_gr_id);
        $chil = $bphc->has_children();
        if (empty($chil))
            return false;
        foreach ($chil as $child => $val) {
            $member = new BP_Groups_Member($user_id, $val);
                   $member->remove();
        }
    }
    
    add_action('groups_leave_group', 'gh_remove_user_when_is_deleted_in_parent_group', 30, 2);
    add_action('groups_removed_member', 'gh_remove_user_when_is_deleted_in_parent_group', 30, 2);
    add_action('groups_remove_member', 'gh_remove_user_when_is_deleted_in_parent_group', 30, 2);
    
    }

    It works for me: WP 3.4.2, BP 1.6.1, BP Group Hierarchy 1.3.4

    http://wordpress.org/extend/plugins/bp-group-hierarchy/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author David Dean

    (@ddean)

    Hi epawel,

    Thanks for contributing this! I haven’t reviewed the code, but I know this is something people ask for. I’ll make it a sticky post so people can find it more easily.

    – David

    I just tried this with WP 3.5.1 and BP-GH 1.3.8 with no success. This is just a preliminary finding, I’ll do some debugging and see what’s going on.

    I was just thinking: maybe this code doesn’t get triggered if members are added via another plugin, i.e. BP Group Manager? Any suggestions on what needs to be done to have this code trigger no matter how a user is added. (Group manager does use invite mechanism.)

    Thanks!
    ~Mike

    Correction: BP – Group Manager does NOT use invite mechanism as far as I can tell — the user doesn’t get an invite, just gets added.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: BP Group Hierarchy] group members automatically added to a subgroup’ is closed to new replies.