Home / Archive / MemberPress: Remove a User Role After Subscription to a Specific Membership Expires
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Remove a User Role After Subscription to a Specific Membership Expires

The code snippet will remove the specified role from a user after the user’s subscription to a specific membership expires.

Replace the dummy membership ID of "123" with the ID of the needed membership on this line:

if ($txn->product_id == '123') {

Next, replace the dummy "custom-role" user role on this line:

$user->remove_role('custom-role');

Replace the dummy user role with the name of the actual user role related to the membership specified in the code.

Code Preview
php
<?php
add_action('mepr-event-subscription-expired', function($event) {
  $txn = $event->get_data();  // Get the transaction data from the event
  $user = $txn->user();       // Get the user associated with the transaction
  // Check if the expired subscription is for the specific membership
  if ($txn->product_id == '123') {
    $user->remove_role('custom-role');  // Remove the specified role from the user - replace the 'custom-role' with the needed user role name
  }
});

Comments

Add a Comment