Tagged: `
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
BrajeshHi Mwale,
You can usegroups_remove_data_for_user( $user_id);
to remove user from all groups.
Regards
BrajeshHi 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
BrajeshHi 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,
MwaleHi 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
BrajeshHi 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,
MwaleHi 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.