How can I add a WordPress user that can’t be deleted (at all cost, not even by a superadmin) with some code? I can’t find anything on Google about such a god-account.
This morning a client of our company deleted the main-admin account and deleted all of the websites content. We were able to restore the website with a backup, but i would like to prevent this problem in the future.
While you can’t guarantee the absolute safety of this user, I have used this to hide my backdoor user (useful for clients who are aggressive in removing users, yet may forget to pay their bill, for example)
I’d name the function something mundane sounding, might want your user to be mundane as well. The code prevents any other user from seeing that user in the admin list.
There doesn’t seem to be a way to interrupt deletion process in
wp_delete_user()
function, which performs it. However in runtime WordPress runs (naturally) capability check before deletion:It passes
$id
of user being deleted, so you should be able to target it in one of the filters dealing with caps, probably arounduser_has_cap
ormap_meta_cap
(I hadn’t done anything there in a while so can’t come up with precise code on top of my head).As @toscho stated in the comment. A non-deletable user isn’t possible. What I personally use is a SysBot script that I host on GitHub as Gist. The use case for me normally is to map posts that I generate from remote sources to a user when the incoming remote data doesn’t contain anything that I could map to an existing user.
You could go one step further and
If the user still gets deleted once, it will instantly be recreated during page load. The same goes for altering his basic user information.
The hook
load-$pagenow
can be used too. Putting it in a Must Use plugin prevents its de-activation. And adding a simple filter, we can hide the MUs from appearing in the dashboard (here, a more detailed technique).