BuddyDev

Search

[Resolved] How to remove member from a groups forum when member is removed from the group?

  • Participant
    Level: Enlightened
    Posts: 88
    Torben Heikel Vinther on #39942

    Hi

    I have a function and an action that fires when a user leaves a group. When he does that he is automatically removed from the forum that belongs to the group. It works fine, but if I manually remove a user from a group in the backend, then he is not removed from the forum!

    Has anyone a solution to this? Maybe I should use the action hook groups_member_after_remove?

    Here is the function that works fine in frontend when the user leaves the group by himself:

    add_action( 'groups_leave_group', 'cci_auto_unsubscribe_forum', 10, 2 );
    
    // function triggered when user leaves group
    function cci_auto_unsubscribe_forum ( $group_id, $user_id ) {
    	// checks to see if there's a user ID
    	if( !$user_id ) return false;
    	// Get's an array of forum IDs for the group
    	$forum_ids = bbp_get_group_forum_ids( $group_id );
    	// checks to see if there is a forum ID
    	if ( !empty( $forum_ids ) ) {
    	// gets the first value from the array, assuming there is one forum
    	$forum_id = array_shift( $forum_ids );
    	}
    	// subscribes user to forum
    	bbp_remove_user_forum_subscription( $user_id, $forum_id );
    }
    

    Thanks

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2909
    Ravi on #39949

    Hello Torben,

    Thank you for posting here. Please try on the following action:

    
    do_action( 'groups_remove_member', $group_id, $user_id );
    

    Let me know if it helps or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 88
    Torben Heikel Vinther on #39950

    Hi Ravi

    Thank you for your answer. I tried to add your line just below the line with “bbp_remove_user_forum_subscription( $user_id, $forum_id );”, but it didn’t solve my problem. The user is still subscribed to the group forum after he is removed from the group by me in the backend.

    Should I insert your line in another place? Eg. as an add_action?

    Regards
    Torben

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2909
    Ravi on #39952

    Hello Torben,

    Please try the following code:

    
    
    add_action( 'groups_remove_member', 'cci_auto_unsubscribe_forum', 10, 2 );
    add_action( 'groups_leave_group', 'cci_auto_unsubscribe_forum', 10, 2 );
    
    // function triggered when user leaves group
    function cci_auto_unsubscribe_forum ( $group_id, $user_id ) {
    	// checks to see if there's a user ID
    	if( !$user_id ) return false;
    	// Get's an array of forum IDs for the group
    	$forum_ids = bbp_get_group_forum_ids( $group_id );
    	// checks to see if there is a forum ID
    	if ( !empty( $forum_ids ) ) {
    	// gets the first value from the array, assuming there is one forum
    	$forum_id = array_shift( $forum_ids );
    	}
    	// subscribes user to forum
    	bbp_remove_user_forum_subscription( $user_id, $forum_id );
    }
    

    Let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 88
    Torben Heikel Vinther on #39953

    You are legend! It works as aspected! Thank you very much!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2909
    Ravi on #39966

    Hello Torben,

    Thank you for the kind words. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] How to remove member from a groups forum when member is removed from the group?’ is closed to new replies.

This topic is: resolved