Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active February 19, 2019 23:24
Show Gist options
  • Save andrewlimaza/1e363602b6d67a942f420010575c96c8 to your computer and use it in GitHub Desktop.
Save andrewlimaza/1e363602b6d67a942f420010575c96c8 to your computer and use it in GitHub Desktop.
Assign a new WP user role when a user cancels their membership in Paid Memberships Pro
<?php
//Please copy line 5-22 into your active themes functions.php or custom plugin
//code to assign specific WP user role when cancelling
function my_pmpro_after_cancel_change_role($level_id, $user_id)
{
//get user object
$wp_user_object = new WP_User($user_id);
//ignore admins
if(in_array("administrator", $wp_user_object->roles)){
return;
}
//if user cancels their membership, set their user role.
if(empty($level_id)) {
$wp_user_object->set_role('subscriber'); //choose your default role.
}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_cancel_change_role", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment