Assign and withdraw role on an user based on WPMU Membership2

WPMU Membership2 plugin has an addon that you can use to assign extra capabilities to an user when he joins in a membership, and when the user left the membership the capabilities are withdrawn. But, the problem is, even it says “Assign role”, it doesn’t assign role to that users but just assign the capabilities of that role.

So, if you need members of certain role, you won’t get those members in the member list. Even, if you have other plugin that makes some operation based on role, won’t work here. Let’s find a solution.

The following snippet will allow you to set different role based on membership, you will need to know membership IDs though. In the following example, if ID is 1343, I want to assign those users “editor” role, “author” for 1345 and “administrator” for 1348. Take a look:


You may also read:  Assign a membership to a registered member automatically - Membership 2 (WPMU)
<?php
add_action( 'ms_model_relationship_create_ms_relationship_before', 'ms_controller_member_assign_memberships_done_cb', 99, 4 );
function ms_controller_member_assign_memberships_done_cb( $membership_id, $user_id, $gateway_id, $move_from_id ) {
$user = new WP_User( $user_id );
switch( $membership_id ){
case 1343:
$user->set_role( 'editor' );
break;
case 1345:
$user->set_role( 'author' );
break;
case 1348:
$user->set_role( 'administrator' );
break;
}
}

view raw

code.php

hosted with ❤ by GitHub

Now, if the user’s membership is deactivated or cancelled, we need to assign back “subscriber” role to that user. I have used same code for cancel and deactivation operation, you can assign different role if you want:


You may also read:  My working experience at WPMU DEV
<?php
add_action( 'ms_model_event', 'my_event_handler', 10, 2 );
/**
* Handles an event and process the correct communication if required.
*
* @param MS_Model_Event $event The event that is processed.
* @param mixed $data The data passed to the event handler.
*/
function my_event_handler( $event, $data ) {
$member = false;
$subscription = false;
$membership = false;
switch ( $event->type ) {
case MS_Model_Event::TYPE_MS_CANCELED:
// A membership was cancelled – either by Admin or by the member.
// No more payments will be made but member has access until current period ends.
$subscription = $data;
$membership = $data->get_membership();
$member = $subscription->get_member();
assign_default_role( $member->id );
break;
case MS_Model_Event::TYPE_MS_DEACTIVATED:
// A membership was permanently deactivated. Member has no access anymore.
$subscription = $data;
$membership = $data->get_membership();
$member = $subscription->get_member();
assign_default_role( $member->id );
break;
}
}
function assign_default_role( $user_id = ){
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
}
You may also read:  How to add an user into a BuddyPress group automatically when he joins in a WPMU membership

view raw

code.php

hosted with ❤ by GitHub

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

You may also like...

2 Responses

  1. xbladerunner says:

    Just discovered your blog, Ashok! Great stuff as usual from you. Obvious limitation in this case is the part about needing to know membership IDs. I’d be very interested in seeing a work-around for that, as it would make the new membership plugin much more widely compatible with other plugins. Thanks for all you do!

    • Ashok Kumar Nath says:

      Thank you for your comment. Knowing membership ID is not very difficult though, you can hover over the “Show” link at the right column in All Memberships page where you can see the membership ID: take.ms/4zZRP

      But, I would like to see a column for membership ID, that will make things more easier to find 🙂

      Thanks again.

Leave a Reply

%d bloggers like this: