Support » Plugin: WordPress Membership Plugin - Restrict Content » How to remove the user’s role at expiration

  • Resolved Pete

    (@perthmetro)


    *This only applies to RCP Pro but I wanted to post it here as there’s no other way to get this out to the masses.

    How to remove the user’s role allocated during subscription after their subscription expires.

    I asked if RCP could do this a few days ago and I was told it couldn’t. I’ve sought some help and they have solved my issue below… I just thought it might be able to be shared with the RCP community. Thanks.

    Source: http://wpquestions.com/Restrict_Content_Pro_remove_user_s_subscription_role/19602

    <strong>// RCP remove role on expire</strong>
    add_action('wp_loaded', 'remove_role_to_expired');
    function remove_role_to_expired(){
    $members = rcp_get_members( 'expired', 1 ); // Subscription ID
    
    if( $members ) {
    foreach ( $members as $member ) {
    $member = new RCP_Member( $member->ID );
    $user_role = 'schoolplus'; // user-role to be deleted (the same one that is allocated during subscription
    $member->remove_role( $user_role );
    }
    }
    } 
    • This topic was modified 4 years, 6 months ago by Pete.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thanks for sharing!

    I would not, however, recommend using this snippet as is. It’s adding a potentially expensive (in terms of performance) query to every page load.

    It would be better to hook into the “rcp_set_status” hook and remove the role when a customer’s status is changed from “active” to “expired”.

    Thread Starter Pete

    (@perthmetro)

    Ooooh, thanks!

    Thread Starter Pete

    (@perthmetro)

    Here’s a fix…

    add_action('rcp_set_status', 'remove_role_to_expired', 30, 2);
    function remove_role_to_expired( $status, $user_id ) {
    	if ( 'expired' == $status ) { // expired status
    		$member = new RCP_Member( $user_id );
    		if ( $member->get_subscription_id() == 1) { // Subscription ID e.g. 1 
    			$user_role = 'XYZ'; // user-role to be deleted
    			$member->remove_role( $user_role );
    		}
    	}
    }

    *please note: once a member’s subscription has expired and the allocated role has been removed, the member can not get that role back again if they re-subscribe. You’ll need to allocate the role back to the member manually using this awesome plugin.

    • This reply was modified 4 years, 6 months ago by Pete. Reason: fixed original code
    • This reply was modified 4 years, 6 months ago by Pete.

    That looks good!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to remove the user’s role at expiration’ is closed to new replies.