Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Restrict Content Pro - remove user's subscription role WordPress

  • SOLVED

Restrict Content Pro - remove user's subscription role when their subscription lapses

When admin creates a "subscription" they can allocate a user-role to that subscription that is applied to all users who join that particular subscription.
[[LINK href="https://snag.gy/CtqiFw.jpg"]]https://snag.gy/CtqiFw.jpg[[/LINK]]

When the user's subscription lapses the user's subscription role is not removed from the user (as it is linked to the subscription, not the user).

I'm looking for a function that removes the user's role (the role that was allocated to their subscription).

*The users will have other user roles before they subscribe so I don't want these original user roles removed, just the role that is allocated to their subscription.

http://www.filedropper.com/restrict-content-pro

[[LINK href="http://docs.pippinsplugins.com/category/41-developer-documentation"]]http://docs.pippinsplugins.com/category/41-developer-documentation[[/LINK]]

https://github.com/restrictcontentpro/restrict-content-pro

Answers (3)

2016-12-12

Reigel Gallarde answers:

May I know why you want to remove the role?

I'm asking because below codes works but with problem...
you can try adding this to you functions.php of current theme.

WARNING! PLEASE MAKE A BACKUP of wp_usermeta table before trying or test only on a test user.
add_action('wp_loaded', 'remove_role_to_expired');
function remove_role_to_expired(){
$members = rcp_get_members( 'expired' );

if( $members ) {
foreach ( $members as $member ) {
$member = new RCP_Member( $member->ID );
$user_role = rcp_get_user_role($member->ID);
$member->remove_role( $user_role );
}
}
}

WARNING! PLEASE MAKE A BACKUP of wp_usermeta table before trying or test only on a test user.


pjeaje comments:

RCP can allocate a new user role for a specific subscription. I'm using this new user role to restrict content in my theme files (I don't want to to use their hierarchical access level).

What is the "problem" with your code?


pjeaje comments:

Does this code specify a specific subscription with it's specific user role?


Reigel Gallarde comments:

the problem is not with my code but with the plugin. if you remove the role, the plugin will use the older role... wither this role is added by the plugin or not it will get removed if the subscription status is expired...

if you have specific role, might be better change
$user_role = rcp_get_user_role($member->ID);
to something like $user_role = 'subscriber';


pjeaje comments:

So even the original role will be removed, as well as the subscription role? I'm happy for you to specify the expired role when the sub is expired...e .g.

original role: school
sub role: school-plus
expired role: school


Reigel Gallarde comments:

yes try $user_role = 'school'; in the code


Reigel Gallarde comments:

actually there's no such subscription role... the plugin is using wp role + capabilities... so we can't really tell if the role is from subscription or from other plugin... the plugin don't have a function to remove a role from subscription...


pjeaje comments:

"the plugin don't have a function to remove a role from subscription"... ahh yes i see :(

like this?
add_action('wp_loaded', 'remove_role_to_expired');
function remove_role_to_expired(){
$members = rcp_get_members( 'expired' );

if( $members ) {
foreach ( $members as $member ) {
$member = new RCP_Member( $member->ID );
$user_role = 'school';
$member->remove_role( $user_role );
}
}
}


pjeaje comments:

But it needs to only apply to a specific sub, not all subs


Reigel Gallarde comments:

if you have the id of the subs, you can change

$members = rcp_get_members( 'expired' );
to
$members = rcp_get_members( 'expired', 1 );

where 1 is the subs id.


pjeaje comments:

like this?
add_action('wp_loaded', 'remove_role_to_expired');
function remove_role_to_expired(){
$members = rcp_get_members( 'expired', 2 );

if( $members ) {
foreach ( $members as $member ) {
$member = new RCP_Member( $member->ID );
$user_role = 'school';
$member->remove_role( $user_role );
}
}
}


pjeaje comments:

So, the above will ONLY remove the 'school' when the sub expires? and keep all the other roles?


pjeaje comments:

Thanks this works!

add_action('wp_loaded', 'remove_role_to_expired');
function remove_role_to_expired(){
$members = rcp_get_members( 'expired', 2 ); // Subscription ID

if( $members ) {
foreach ( $members as $member ) {
$member = new RCP_Member( $member->ID );
$user_role = 'school'; // user-role to be deleted
$member->remove_role( $user_role );
}
}
}

2016-12-12

Arnav Joy answers:

Hi ,
This seems doable ..


pjeaje comments:

I will not work on this task outside this forum. Please add all code and correspondence via this website only. Thanks.


Arnav Joy comments:

I think this will work please try

if(rcp_is_expired( $user_ID )) {
$member = new RCP_Member( $user_ID );
$member->remove_role( $role );
}


pjeaje comments:

You need to apply it to a specific subscription


pjeaje comments:

all users will already have a user role before the subscribe and get a second user role. I only want the new role removed, not their original role/s

2016-12-12

Krishna Tiwari answers:

HI,
Could you confirm when subscription is expired then what user role set?

Regards,
Krishna


pjeaje comments:

Sorry I don't understand your question


pjeaje comments:

When the subscription is expired the user role is removed. All users already have other user roles prior to subscribing.


Arnav Joy comments:

you can try this


add_action('wp_loaded','aj_process_role');
function aj_process_role(){

$role_to_remove = 'supportstaff';
$subs_id_to_check = 1;

$user_id = get_current_user_id();
$subscription_id = rcp_get_subscription_id( $user_id );

if( rcp_is_expired( $user_id ) ) {
$member = new RCP_Member( $user_id );
$member->remove_role( $role_to_remove );
}

}


Please note that you have to change these two values as per you need

$role_to_remove = 'supportstaff';
$subs_id_to_check = 1;


pjeaje comments:

Thanks, i'll try it. the minimum period is 1 day so it will take a while to test.


pjeaje comments:

I tested it by going into the back end and manually subscribing a user, then manually expiring that user's subscription but the role was removed. :(


pjeaje comments:

but the role wasn't* removed. :(