Remove the Deactivate and Delete links for plugins your site depends on, and quietly keep them active if someone tries anyway.
function nahnu_locked_plugins(): array {
return [
'woocommerce/woocommerce.php',
'nahnu-login-shield/nahnu-login-shield.php',
];
}
// Hide Deactivate and Delete on the Plugins screen.
add_filter( 'plugin_action_links', function ( array $actions, string $plugin_file ): array {
if ( in_array( $plugin_file, nahnu_locked_plugins(), true ) ) {
unset( $actions['deactivate'], $actions['delete'] );
$actions['nahnu_locked'] = '<span style="color:#6B6680">Required by this site</span>';
}
return $actions;
}, 10, 2 );
// Keep them active if they're deactivated in bulk or by another tool.
add_filter( 'pre_update_option_active_plugins', function ( $new, $old ) {
if ( defined( 'NAHNU_UNLOCK_PLUGINS' ) && NAHNU_UNLOCK_PLUGINS ) {
return $new;
}
foreach ( nahnu_locked_plugins() as $plugin ) {
if ( in_array( $plugin, (array) $old, true ) && ! in_array( $plugin, (array) $new, true ) && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
$new[] = $plugin;
}
}
return $new;
}, 10, 2 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
On client sites, deactivating the wrong plugin, such as WooCommerce or a security plugin, can take the site down or open a hole. This snippet protects a short list of plugins from being switched off by accident.
The plugin_action_links filter removes the Deactivate and Delete links for the listed plugins and adds a “Required by this site” note.
The list of active plugins is stored in the active_plugins option. pre_update_option_active_plugins runs before it’s saved, and puts back any locked plugin that was removed, so bulk actions and other tools can’t switch them off either.
define( 'NAHNU_UNLOCK_PLUGINS', true ); to wp-config.php temporarily.folder/file.php.Open Plugins. WooCommerce should show “Required by this site” instead of Deactivate. Try a bulk deactivate: it should stay active.