WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/delete user

Description

The delete_user action/hook can be used to perform additional actions when a user is deleted. For example, you can delete rows from custom tables created by a plugin.

The hook passes two parameters:

  • $id: ID the user being deleted
  • $reassign: ID of the user to whom posts will be automatically re-assigned (if one was selected)

This hook runs before a user is deleted. The hook deleted_user (notice the "ed") runs after a user is deleted. Choose the appropriate hook for your needs. If you need access to user meta or fields from the user table, use delete_user.

Users deleted from Network Site installs may not trigger this hook. Be sure to use the wpmu_delete_user hook for those cases. The deleted_user hook is called in either case.

Usage

<?php add_action( 'delete_user', 'function_name' ); ?>

where "function_name" is the name of the function to be called.

Example: Email Users Being Deleted

Let's have a look at an example:

function my_delete_user( $user_id ) {
	global $wpdb;

        $user_obj = get_userdata( $user_id );
        $email = $user_obj->user_email;

	$headers = 'From: ' . get_bloginfo( "name" ) . ' <' . get_bloginfo( "admin_email" ) . '>' . "\r\n";
 	 wp_mail( $email, 'You are being deleted, brah', 'Your account at ' . get_bloginfo("name") . ' is being deleted right now.', $headers );
}
add_action( 'delete_user', 'my_delete_user' );

In this example we send a friendly message to a user before their account is deleted a few milliseconds later.

Related

See Also

This page is marked as incomplete. You can help Codex by expanding it.