Email Alert When a User Account Is Deleted
Emails the site admin when any user account is deleted, including who deleted it and whether their content was reassigned, so removals never go unnoticed.
Opens WPIDE on
Code
add_action( 'delete_user', function ( $user_id, $reassign = null ) {
$user = get_userdata( $user_id );
$actor = wp_get_current_user();
if ( ! $user ) {
return;
}
$reassigned = $reassign ? get_userdata( (int) $reassign ) : false;
wp_mail(
get_option( 'admin_email' ),
sprintf( '[%s] User deleted: %s', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $user->user_login ),
sprintf(
"The account %s <%s> (%s) was deleted by %s on %s.\nContent: %s",
$user->user_login,
$user->user_email,
implode( ', ', (array) $user->roles ),
$actor->exists() ? $actor->user_login : 'an automated process',
wp_date( 'Y-m-d H:i' ),
$reassigned ? 'reassigned to ' . $reassigned->user_login : 'deleted with the account'
)
);
}, 10, 2 );
Notes
Works for single sites; on multisite network user deletions use a different hook.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.