WordPress: Prevent Your Account From Being Deleted By Admins

wordpress prevent user deletion

Let’s say you have a few admins on your WordPress site. You’re probably the owner of the site, and maybe you have your developers, your designers, your content writers, and maybe other staff that manage the site as well. If you give an admin access to your developers and designers, it means with that same power, they could accidentally (or intentionally) delete your account too.

So how do you prevent this from happening?

You can’t, not really. But you can at least make it a little harder for them to do so, given that they don’t have access to edit your files, database, or access to FTP.

Or, how about this. What if you are an honest developer who’s trying to get your client to pay after you hand over a fully functional site to them, but they are just trying to get your service for free and ghost you. Obviously their next step includes deleting you from their site. You could prepare for this by preventing your account from being deleted until your client pays. Of course your client can remove the codes, but they would have to know what to remove first, wouldn’t they? I’m not recommending doing this though, just one of the many scenarios the following code snippets could be useful!

Use the code below to prevent your account from being deleted by admins. Drop it into your theme’s functions.php file, or use a code snippets plugin, or make it into its own plugin, your choice.

function wproot_prevent_user_deletion( $deleting_userid ) {
$protected_userid = 1; //change 1 to the ID of the user you don't want to be deleted.
if ( $deleting_userid == $protected_userid ) {
$user_obj = get_user_by('id', $deleting_userid);
$name = $user_obj->user_login;
wp_die( sprintf( 'Sorry, you are not allowed to delete user %s.', $name ) );
add_action('delete_user', 'wproot_prevent_user_deletion');
Blog post URL: https://wproot.dev/blog/wordpress-how-to-prevent-your-account-from-being-deleted-by-admins/

Change the ID in line 4 in the code above (the number “1” you see in that line) to the ID of the user you don’t want to be deleted. Read this in case you don’t know how to find your user ID.

In the above code, we hook to the delete_user hook that fires immediately before a user is deleted from the database. We output a notice to let the person know they can’t delete the user and abort the script.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.