# Protect Specific Users From Editing and Deletion

> Stops anyone but the users themselves from editing, demoting or deleting the accounts you list, such as the site owner or an agency account.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: users, protection, security
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/protect-users-from-changes/

## Code

```php
// IDs of protected users. Find an ID by hovering the user's name on the Users screen.
$wpide_protected_users = array( 1 );

add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) use ( $wpide_protected_users ) {
    if ( ! in_array( $cap, array( 'edit_user', 'delete_user', 'remove_user', 'promote_user' ), true ) || empty( $args[0] ) ) {
        return $caps;
    }

    $target = (int) $args[0];

    if ( in_array( $target, array_map( 'intval', $wpide_protected_users ), true ) && $target !== (int) $user_id ) {
        $caps[] = 'do_not_allow';
    }

    return $caps;
}, 10, 4 );
```

## Notes

Protected users can still edit their own profile.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Protect Specific Users From Editing and Deletion" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
