Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 16, 2021 01:15
Show Gist options
  • Save strangerstudios/2185226 to your computer and use it in GitHub Desktop.
Save strangerstudios/2185226 to your computer and use it in GitHub Desktop.
Delete the WordPress User When a Paid Memberships Pro Member Cancels His Account
/*
Requires PMPro v1.4+
Code to delete WP user accounts when a member cancels their PMPro account.
Users are not deleted if:
(1) They are not cancelling their membership (i.e. $level_id != 0)
(2) They are an admin.
(3) The level change was initiated from the WP Admin Dashboard
(e.g. when an admin changes a user's level via the edit user's page)
*/
function my_pmpro_after_change_membership_level($level_id, $user_id)
{
//are they cancelling? and don't do this from admin (e.g. when admin's are changing levels)
if(empty($level_id) && !is_admin())
{
//only delete non-admins
if(!user_can($user_id, "manage_options"))
{
//remove the delete hooks so we don't try to cancel the membership again
remove_action('delete_user', 'pmpro_delete_user');
remove_action('wpmu_delete_user', 'pmpro_delete_user');
//delete the user
require_once(ABSPATH . "/wp-admin/includes/user.php");
wp_delete_user($user_id);
//add the delete hooks back in
add_action('delete_user', 'pmpro_delete_user');
add_action('wpmu_delete_user', 'pmpro_delete_user');
}
}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
@unwiredtech
Copy link

Hello,

This add-on is still application in the latest version?

I tried to cancel a subscription but the post (custom post type called resume) of the user is still available after the account had cancel the subscription.

Regards,

@BAG-OF-CHIPS-XX
Copy link

Where do I put this code?
Can I add the code to my "PMPro customizations" plugin?
Any help is appreciated.
Thanks!

@rgw2
Copy link

rgw2 commented Oct 24, 2019

Used this and still works perfectly. Thanks!
In answer to previous commenter, you place in functions.php in theme editor.

@laurenhagan0306
Copy link

This recipe is included in the blog post on "Delete the WordPress user when a PMPro member cancels" at Paid Memberships Pro here: https://www.paidmembershipspro.com/delete-the-wordpress-user-when-a-paid-memberships-pro-member-cancels-his-account/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment