How to Disable Plugin Deactivation for Specific Plugins in WordPress

WPTurbo » Snippets » How to Disable Plugin Deactivation for Specific Plugins in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever encountered a situation where you wanted to prevent certain plugins from being deactivated on your WordPress site? Maybe you have essential plugins that need to be constantly active for your website to function properly, or you’re using custom-built plugins that should never be disabled by mistake. In this article, we will explore how you can disable the plugin deactivation feature for specific plugins in WordPress, giving you more control over your site’s functionality and ensuring that critical plugins are always active.

					function wpturbo_disable_plugin_deactivation( $actions, $plugin_file, $plugin_data, $context ) {
    $plugins = array( 'plugin1/plugin1.php', 'plugin2/plugin2.php' ); // Add your plugin slugs here
    
    if (in_array($plugin_file, $plugins)) {
        unset( $actions['deactivate'] );
    }
    
    return $actions;
}
add_filter( 'plugin_action_links', 'wpturbo_disable_plugin_deactivation', 10, 4 );
				

The code snippet above shows how to disable the ability to deactivate specific plugins in WordPress. This can be useful if you want to prevent users from accidentally deactivating important plugins on your website.

The wpturbo_disable_plugin_deactivation() function takes in four parameters: $actions, $plugin_file, $plugin_data, and $context. These parameters are automatically passed to the function by WordPress when the plugin_action_links filter hook is triggered.

Inside the function, we define an array called $plugins which contains the slugs of the plugins that we want to disable deactivation for. You can add the slugs of your desired plugins to this array. For example, if you want to disable deactivation for a plugin with the slug plugin1/plugin1.php and another with the slug plugin2/plugin2.php, you would add them to the array like this:

$plugins = array( 'plugin1/plugin1.php', 'plugin2/plugin2.php' );

Next, we use the in_array() function to check if the current plugin being processed matches any of the plugins in the $plugins array. If it does, we unset the 'deactivate' action from the $actions array. This effectively removes the "Deactivate" link from the plugin’s action links in the WordPress admin.

Finally, we return the modified $actions array, which will be used by WordPress to generate the plugin action links.

To implement this functionality, we hook the wpturbo_disable_plugin_deactivation() function to the plugin_action_links filter using the add_filter() function. The fourth parameter 10 denotes the priority of the filter and the last parameter 4 indicates the number of arguments that the filter function expects.

By using this code snippet, you can prevent specific plugins from being deactivated by removing the "Deactivate" link from their action links in the WordPress admin.

Register an account to save your snippets or go Pro to get more features.