# Prevent Deactivating Essential Plugins

> Removes the Deactivate option for the plugins you list, for every user, so key functionality cannot be switched off by accident.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: security, plugins, client
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/prevent-deactivating-essential-plugins/

## Code

```php
// Plugin files that must stay active (folder/main-file.php).
$wpide_essential_plugins = array( 'woocommerce/woocommerce.php' );

add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) use ( $wpide_essential_plugins ) {
    if ( in_array( $cap, array( 'deactivate_plugin', 'delete_plugins' ), true ) && ! empty( $args[0] ) && in_array( $args[0], $wpide_essential_plugins, true ) ) {
        $caps[] = 'do_not_allow';
    }

    return $caps;
}, 10, 4 );

add_filter( 'plugin_action_links', function ( $actions, $plugin_file ) use ( $wpide_essential_plugins ) {
    if ( in_array( $plugin_file, $wpide_essential_plugins, true ) ) {
        unset( $actions['deactivate'] );
        $actions['wpide_locked'] = '<span style="color:#646970">Required</span>';
    }

    return $actions;
}, 10, 2 );
```

## Notes

The plugin can still be deactivated through FTP or WP-CLI.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Prevent Deactivating Essential Plugins" 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/
