How-To Wordpress

How to use delete_user hook in WordPress

In this tutorial, we’ll set up a method for allowing our Users to do some other actions while performing the delete option which is available in WordPress. The main aim of this article is to delete any other unwanted values(user’s other data Example: User second Name, User Address) of the Consumer/Customer which will create more sizes in our DataBase.

Usually, most websites require user pieces of information like First Name, Middle Name, Last Name, Email ID, Phone Number, Address1, Address2 Etc. To add all these fields in registration forms, they may need third-party Plugins. These third-party plugins won’t delete these data when you delete a WordPress user account. In this case, exponentially the DataBase size will increase. To avoid this, we have found some codes which will be helpful in deleting these junk values.

Here is the method :


function my_delete_user( $user_id ) {
// DB connection
global $wpdb;
$delete_usernickname = 'DELETE FROM wp_usermeta WHERE meta_key= 'nickname' and userid = "'.$user_id.'"';
$wpdb->query($delete_usernickname);
}
add_action('delete_user', 'my_delete_user');

In the above code, we are taking the user_id dynamically in the function so that, on click of the delete button, User Id will come under this function, and it fires up an action which is written in the next line.

In the below image we can see, onClick of which button this hook will fire up. WordPress/Admin Dashboard/Users – List Table
Delete_User_image

If you have any queries regarding the above method, please write them in the below comment box.

Happy Coding 🙂

About the author

Vishwas

A coder who is trying to solve some problems via "Procoders", Software Engineer By Profession, WEB Enthusiast, Hobby Blogger. I love to share Tech Ideas and like to learn and explore new things and Technologies.

Add Comment

Click here to post a comment