Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Can I make it impossible for a user account to be deleted? WordPress

  • SOLVED

I was able to get the permissions set up the way I needed. Thanks to everyone who helped on my last question. Now I'm wondering, can I make it impossible to delete certain user accounts? I don't want even the admin to be able to delete certain user accounts. Right now, I've got a fragile set up of user privileges. Mistaken deletions would upset the system. If I can't block user accounts from being deleted, can I make it so that they automatically get saved somewhere, so that they can be restored? Is there a way to save stuff on delete, in WordPress?

Japh, to answer your question, it is just certain users that I need to keep safe. I am using a lot of custom fields on this project, and I feel the setup is fragile. I worry about someone deleting a user, and then not knowing how to re-create that user, with all of the right info in the custom fields.

Answers (4)

2010-01-04

Justin Tadlock answers:

You can add this to your theme's functions.php file:

add_action( 'delete_user', 'dont_delete_user' );

function dont_delete_user( $id ) {

$dont_delete_ids = array( 1, 2, 3, 4 );

if ( in_array( $id, $dont_delete_ids ) )
wp_die( 'You cannot delete this user account.' );
}


The most important line is this one:

$dont_delete_ids = array( 1, 2, 3, 4 );

Add any user IDs that you don't want deleted to that array.

This code will create an error page when a user account is trying to be deleted from the WordPress admin if you've added the user ID.

2010-01-04

Dan Davies answers:

I'm not sure of any way of preventing users from being deleted. You could perhaps edit the core files and remove the delete button, or use some CSS to hide it?

For backup methods, why not consider just a standard database backup, something that can be provided with this plugin: http://wordpress.org/extend/plugins/wp-db-backup/

2010-01-04

Andrew Jones answers:

It's possible for a plug-in to extend the Users page. From there, you could pass in an array of user-ids and do whichever with them including disabling the "Delete" function. That way you don't have to muck about in the core and have your solution killed by a WP upgrade.

2010-01-04

Japh answers:

paddy, do you want to block the deletion of ALL user accounts or only "certain" ones? if only certain ones, what are the criterion for when a user can or can't be deleted?

if you can give this information, we can more easily provide you with some sample code, rather than just directions :)