Last active
April 16, 2021 01:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/