Skip to content

Instantly share code, notes, and snippets.

@userabuser
Last active April 14, 2018 10:19
Show Gist options
  • Save userabuser/5617741 to your computer and use it in GitHub Desktop.
Save userabuser/5617741 to your computer and use it in GitHub Desktop.
Perform an action after a user is deleted with access to additional user data (role, name, email, etc) in addition to user ID. (WordPress)
<?php
/*
* The deleted_user hook fires after a user is deleted and receives
* only one arguement, the $user_id but there is no way to check,
* based on the $user_id, if the user belongs to a specific user role
* as the user no longer exists. Solution: add deleted_user action within
* the delete_user callback function which fires prior to a user being
* deleted and inject a closure on the deleted_user callback.
*
* http://wordpress.stackexchange.com/q/99959/13418
* WordPress, Requires PHP 5.3+
*/
function after_delete_user($user_id) {
$user = get_user_by('id', $user_id);
$role = $user->roles[0];
if ($role == 'subscriber') {
add_action("deleted_user", function(){
//your logic here... (e.g. redirect, notify admin, etc)
});
}
}
add_action("delete_user", "after_delete_user");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment