Skip to:
Content
Pages
Categories
Search
Top
Bottom

groups_member_before_save for restricting one group per user


  • paragbhagwat
    Participant

    @paragbhagwat

    Hello,

    I want to restrict memberships to only one private group and multiple public groups. I was thinking of using the hook groups_member_before_save to do that validation. Is that the correct place?

    This is how i researched..

    in groups_join_group we have

    
    $new_member                = new BP_Groups_Member;
    	$new_member->group_id      = $group_id;
    	$new_member->user_id       = $user_id;
    	$new_member->inviter_id    = 0;
    	$new_member->is_admin      = 0;
    	$new_member->user_title    = '';
    	$new_member->date_modified = bp_core_current_time();
    	$new_member->is_confirmed  = 1;
    
    	if ( !$new_member->save() )
    		return false;
    

    and in BP_Groups_Member we have

    
    public function save() {
    		global $wpdb;
    
    		$bp = buddypress();
    
    		$this->user_id       = apply_filters( 'groups_member_user_id_before_save',       $this->user_id,       $this->id );
    		$this->group_id      = apply_filters( 'groups_member_group_id_before_save',      $this->group_id,      $this->id );
    		$this->inviter_id    = apply_filters( 'groups_member_inviter_id_before_save',    $this->inviter_id,    $this->id );
    		$this->is_admin      = apply_filters( 'groups_member_is_admin_before_save',      $this->is_admin,      $this->id );
    		$this->is_mod        = apply_filters( 'groups_member_is_mod_before_save',        $this->is_mod,        $this->id );
    		$this->is_banned     = apply_filters( 'groups_member_is_banned_before_save',     $this->is_banned,     $this->id );
    		$this->user_title    = apply_filters( 'groups_member_user_title_before_save',    $this->user_title,    $this->id );
    		$this->date_modified = apply_filters( 'groups_member_date_modified_before_save', $this->date_modified, $this->id );
    		$this->is_confirmed  = apply_filters( 'groups_member_is_confirmed_before_save',  $this->is_confirmed,  $this->id );
    		$this->comments      = apply_filters( 'groups_member_comments_before_save',      $this->comments,      $this->id );
    		$this->invite_sent   = apply_filters( 'groups_member_invite_sent_before_save',   $this->invite_sent,   $this->id );
    
    		/**
    		 * Fires before the current group membership item gets saved.
    		 *
    		 * Please use this hook to filter the properties above. Each part will be passed in.
    		 *
    		 * @since 1.0.0
    		 *
    		 * @param BP_Groups_Member $this Current instance of the group membership item being saved. Passed by reference.
    		 */
    		do_action_ref_array( 'groups_member_before_save', array( &$this ) );
    
    		// The following properties are required; bail if not met.
    		if ( empty( $this->user_id ) || empty( $this->group_id ) ) {
    			return false;
    		}
    
Viewing 4 replies - 1 through 4 (of 4 total)

  • paragbhagwat
    Participant

    @paragbhagwat

    Hello,

    Any one had a chance to read this and confirm this is the place where this can be done?

    Thanks,
    Parag


    Henry Wright
    Moderator

    @henrywright

    After a particular member has reached the 1 private group limit, you’d probably want to stop the member:

    • requesting to join a new private group
    • from being invited to join a private group
    • from creating a new private group

    You mention the member can belong to multiple public groups so your checks should be specific for private groups (also note there is a hidden group type).

    With reference to what hook to use, you’d probably need to use 3 different hooks depending on the actions mentioned above. Take a look through bp-groups/bp-groups-functions.php to start with to see what’s available.


    paragbhagwat
    Participant

    @paragbhagwat

    Thanks @henrywright for requesting to join a new private group i am using the groups_member_before_save hook and it seems to work fine except that any echo or _e or bp_add_message calls seem to be ignored. Any idea what is going on?


    paragbhagwat
    Participant

    @paragbhagwat

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘groups_member_before_save for restricting one group per user’ is closed to new replies.
Skip to toolbar