BuddyDev

Search

[Resolved] How to remove buddypress user from all groups

Tagged: 

  • Participant
    Level: Enlightened
    Posts: 21
    Mwale on #13833

    Hello, I want when users of ‘Free subscription plan’ to be removed from all groups they have joined. What code can I use to remove buddypress user from all the groups he/she has joined.

    Thanks,
    Mwale.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #13835

    Hi Mwale,
    Thank you for posting.

    How are you managing the subscription? What is the AP{I to check the user’s subscription?

    If you are usig a subscription plugin, do they provide an action on expiry of the subscription?

    treagrds
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 21
    Mwale on #13841

    Hi Brajesh,

    Thanks for your reply.

    I am using Membership 2 pro. When user professional plan expires they are downgraded to free plan with id 123.

    $member = MS_Model_Member::get_current_member();
    
    if ( $member->has_membership( 123 ) ) {
        return true;
    }
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #13858

    Hi Mwale,
    You can use

    
    groups_remove_data_for_user( $user_id);
    
    

    to remove user from all groups.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 21
    Mwale on #13859

    Hi Brajesh,

    Thank you so much Brajesh :)) ! Please help me to put the two pieces of code together. I am not very good with PHP.

    Kind regards,
    Mwale.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #13894

    Hi MWale,

    In your case, the best way will be to trigger removal when the membership is deactivated

    Most probably the following code should work.

    
    
    function mwale_remove_from_group_on_membership_deactivate( $event, $subscription ) {
    	$member = $subscription->get_member();
    	$user   = $member->get_user();
    	if ( ! $user || is_a( $user, 'WP_User' ) ) {
    		return;
    	}
    
    	groups_remove_data_for_user( $user->ID );
    
    }
    
    add_action( 'ms_model_event_deactivated', 'mwale_remove_from_group_on_membership_deactivate', 10, 2 );
    
    

    I don’t use the membership2 plugin, so I might have used incorrect api in the following

    
    $member = $subscription->get_member();
    $user   = $member->get_user();
    

    If you can check with their support, that will be nice.

    Try the above code and try cancelling a user’s membership. Does that work?

    Best Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 21
    Mwale on #14023

    Hi Brajesh,

    Thanks for your reply.

    Unfortunately the above did not work. I also tried the following which did not work either 🙁 :

    
        function mwale_remove_from_group_on_membership_deactivate( $event, $subscription ) {
        	$member = MS_Model_Member::get_current_member();
    
    if ( $member->has_membership( 943 ) ) {
        return;
    }
    
        	groups_remove_data_for_user( $user->ID );
    
        }
    
        add_action( 'ms_model_event_deactivated', 'mwale_remove_from_group_on_membership_deactivate', 10, 2 );

    I sent a message to Membership 2 author and I am waiting for a reply.

    In the mean time I found this article: https://premium.wpmudev.org/forums/topic/how-to-check-if-a-user-belongs-to-a-subscription-level

    Kind regards,
    Mwale

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #14028

    Hi Mwale,
    It will be a bad idea to remove user by checking on each page request. That’s why I have selected the deactivated action.

    The deactivated action fires when a user’s subscription expired. That way, It will only work for future expiring membership not the already expired.

    I will suggest checking by expiring a user membership.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 21
    Mwale on #14107

    Hi Brajesh,

    thank you, thank you, thank you so much for your assistance!!!! * 100 🙂

    I almost have a solution and will really appreciate more feedback.

    As you know I want to remove from groups all members with id=943 and no membership. The plugin developers provided this: https://gist.github.com/wpmudev-sls/e3d62705d8caabe74fc410eb1e2e1c7e

    I added this id but nothing happens:

    $membership_ids = array(943);

    Should I add the code you mentioned above? What else do I need to change?

    Kind regards,
    Mwale

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #14124

    Hi Mwale,
    The code you have linked is very inefficient. It will do the processing/checking on each page load which is not a great strategy.

    I will suggest not using the code in its current form.

    Also, The solution should be simple like I showed in my previous example, it is all a matter of knowing what action fires when the membership expires/deactivates.

    Regards
    Brajesh

The topic ‘ [Resolved] How to remove buddypress user from all groups’ is closed to new replies.

This topic is: resolved