3

I have tried to find similar question. And I have not get any existing one. I want to prevent deleting administrators from the back end users table. only two admins can delete all users, But other admins cant delete all other administrators. I have tried this code. But if i go with direct url parameters, its allowing me to delete the user.

function kv_admin_deactivate_link($actions, $user_object) {

    if($user_object->ID == 1 || $user_object->ID == 2) 
        unset($actions['delete']);

    return $actions;
}
add_filter('user_row_actions', 'kv_admin_deactivate_link', 10, 2);

which actually helps to hide the delete link from the users table. but if i go with direct GET link its allowing me to delete.

So is there any function or feature, which will prevent the deletion.

  • simple solution, don't make them admins? kinda the whole point of an admin is to be able to do everything – Mark Kaplun Jan 26 '16 at 14:10
  • i understand it, But he is the actual site user, he needs all the access, except deleting my user account, becuase he deleted it before without his knowledge. so he told me to keep it un deletable account for him and me. so need this feature. Any other suggestions or solution for this task – Kvvaradha Jan 26 '16 at 14:35
7

A quick (and dirty) solution would be to prevent the final deletion where it happens (function delete_user). You could implement a little plugin or paste the code into your functions.php:

<?php

/*
Plugin Name: Please don't delete me!
Description: Prevent accidental user deletion of my account
*/

define('PDDM_USER_ID', 1); // User ID of your Account

add_action('delete_user', function($id) {
    if ($id == PDDM_USER_ID) {
        die('please don\'t delete me!');
    }
});

This just stops the script execution just before your user get's deleted.

Not fancy and pretty ... but it works ;-)

br from Salzburg!

  • 1
    With this kind of logic I would put the priority of the hook higher to "0", just to be sure no other hook will be run then. – Mark Kaplun Jan 26 '16 at 18:57
  • @jjarolim, you done it. its pretty simple , i have not got in mind, its working fine. – Kvvaradha Jan 26 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.