WordPress.org

Ready to get started?Download WordPress

Forums

BP Group Hierarchy
group members automatically added to a subgroup (5 posts)

  1. epawel
    Member
    Posted 1 year ago #

    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/

  2. David Dean
    Member
    Plugin Author

    Posted 1 year ago #

    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

  3. mikebronner
    Member
    Posted 1 year ago #

    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.

  4. mikebronner
    Member
    Posted 1 year ago #

    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

  5. mikebronner
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.