The deleted_user WordPress PHP action fires immediately after a user is deleted from the database.
Usage
add_action('deleted_user', 'your_custom_function', 10, 3); function your_custom_function($id, $reassign, $user) { // your custom code here }
Parameters
- $id (int): ID of the deleted user.
- $reassign (int|null): ID of the user to reassign posts and links to. Default is null, for no reassignment.
- $user (WP_User): WP_User object of the deleted user.
More information
See WordPress Developer Resources: deleted_user
Examples
Log deleted user information
Log the deleted user’s information in a custom log file.
add_action('deleted_user', 'log_deleted_user', 10, 3); function log_deleted_user($id, $reassign, $user) { // Log the deleted user's information error_log("User ID {$id} with email {$user->user_email} was deleted."); }
Send an email notification
Send an email to the site administrator when a user is deleted.
add_action('deleted_user', 'email_admin_deleted_user', 10, 3); function email_admin_deleted_user($id, $reassign, $user) { // Send an email notification to the site administrator wp_mail(get_bloginfo('admin_email'), 'User Deleted', "User ID {$id} with email {$user->user_email} was deleted."); }
Reassign posts and links to another user
Reassign the deleted user’s posts and links to a specified user.
add_action('deleted_user', 'reassign_posts_and_links', 10, 3); function reassign_posts_and_links($id, $reassign, $user) { // Reassign posts and links to user with ID 5 global $wpdb; $wpdb->update($wpdb->posts, array('post_author' => 5), array('post_author' => $id)); $wpdb->update($wpdb->links, array('link_owner' => 5), array('link_owner' => $id)); }
Remove user meta data
Clean up any custom user meta data associated with the deleted user.
add_action('deleted_user', 'remove_custom_user_meta', 10, 3); function remove_custom_user_meta($id, $reassign, $user) { // Remove custom user meta data delete_user_meta($id, 'custom_meta_key'); }
Perform custom action on user deletion
Perform a custom action when a user is deleted, for example, remove the user from a third-party service.
add_action('deleted_user', 'custom_action_on_user_deletion', 10, 3); function custom_action_on_user_deletion($id, $reassign, $user) { // Remove the user from a third-party service your_third_party_api_remove_user($user->user_email); }