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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.