Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.