Deleting a user in WordPress using wp_delete_user

The wp_delete_user function in WordPress is used to delete a user from the database. This can be useful when you want to remove a user from your website, along with all of their associated data, such as posts, comments, and meta data.

By using the wp_delete_user function, you can easily remove a user and all of their content from your website, without having to manually delete each item separately.

Parameters accepted by the WordPress wp_delete_user function

The wp_delete_user function accepts the following parameters:

  • $id (int) – required. This is the User ID.
  • $reassign (int) – optional. Default value is null. This parameter is used to reassign posts and links to a new User ID.

Value returned by the WordPress wp_delete_user function

The wp_delete_user function returns a boolean value. It returns True when the process of deleting the user is finished.

Examples

How to delete a user in WordPress

Below is an example of how to use the wp_delete_user function to delete a user in WordPress.

$user_id = 123;
$result = wp_delete_user( $user_id );
if ( $result ) {
 echo 'User deleted successfully';
} else {
 echo 'Failed to delete user';
}

This code snippet first sets the $user_id variable to the ID of the user to be deleted. Then, it calls the wp_delete_user function with the user ID as a parameter. If the function returns a truthy value, it means the user was deleted successfully, and a success message is echoed. Otherwise, a failure message is echoed.

How to delete a user and reassign their posts to another user

Below is an example of how to use the wp_delete_user function to delete a user in WordPress and reassign their posts to another user.

$user_id = 123;
$reassign_user_id = 456;
$result = wp_delete_user( $user_id, $reassign_user_id );
if ( $result ) {
 echo 'User deleted and posts reassigned successfully';
} else {
 echo 'Failed to delete user';
}

This code snippet sets the $user_id variable to the ID of the user to be deleted and the $reassign_user_id variable to the ID of the user to whom the posts will be reassigned. Then, it calls the wp_delete_user function with both user IDs as parameters. If the function returns a truthy value, it means the user was deleted and their posts were reassigned successfully, and a success message is echoed. Otherwise, a failure message is echoed.

Conclusion

In conclusion, the wp_delete_user function is a powerful feature for removing users from a WordPress site. It allows for the deletion of both the user’s account and all associated content, providing a comprehensive solution for site administrators. However, it is important to use this function with caution, as it is a permanent action that cannot be undone. Additionally, developers should be mindful of any customizations or plugins that may interact with the function to ensure that it behaves as intended. Overall, when used thoughtfully and with proper consideration, the wp_delete_user function can be a valuable asset for managing user accounts in WordPress.

Related WordPress Functions