How to Disable Plugin Deactivation in WordPress
In some cases, you may want to prevent certain plugins from being deactivated by other users or administrators of your WordPress site. This could be to ensure the functionality or security of specific plugins that are crucial for your website. In this tutorial, we will walk you through the process of creating a WordPress plugin that disables the deactivation option for selected plugins.
Step 1: Create a New Plugin Folder
The first step is to create a new folder in the wp-content/plugins/
directory of your WordPress installation. You can name this folder anything you like, but for this tutorial, let’s call it “disable-plugin-deactivation”.
Step 2: Create the Plugin File
Inside the “disable-plugin-deactivation” folder, create a new PHP file and name it “disable-plugin-deactivation.php”. This will be the main file for our plugin.
Step 3: Add Plugin Information
Open the “disable-plugin-deactivation.php” file in a text editor and add the following code at the top:
<?php
/*
Plugin Name: Disable Plugin Deactivation
Plugin URI: https://www.example.com/
Description: Disables the deactivation of other plugins.
Version: 1.0
Author: Your Name
Author URI: https://www.example.com/
License: GPL2
*/
Make sure to replace “Your Name” with your actual name or the name of your organization, and update the Plugin URI and Author URI with relevant URLs.
Step 4: Add Plugin Functionality
Inside the “disable-plugin-deactivation.php” file, paste the following code after the plugin information:
function disable_plugin_deactivation($actions, $plugin_file, $plugin_data, $context)
{
// Specify the plugin slugs that you want to prevent deactivation for
$protected_plugins = array(
'plugin1/plugin1.php',
'plugin2/plugin2.php'
);
if (in_array($plugin_file, $protected_plugins)) {
// Remove the "Deactivate" action from the plugin row
unset($actions['deactivate']);
}
return $actions;
}
add_filter('plugin_action_links', 'disable_plugin_deactivation', 10, 4);
In the $protected_plugins
array, specify the slugs of the plugins that you want to protect from deactivation. Make sure to replace ‘plugin1/plugin1.php’ and ‘plugin2/plugin2.php’ with the actual plugin slugs you want to protect.
Step 5: Activate the Plugin
Save the changes to the “disable-plugin-deactivation.php” file and close it. Now, go to your WordPress admin area and navigate to “Plugins” ยป “Installed Plugins”. You should see the “Disable Plugin Deactivation” plugin listed among the inactive plugins.
Click the “Activate” link below the plugin name to activate it. Once activated, the “Deactivate” link will no longer be available for the plugins specified in the $protected_plugins
array.
Conclusion
In this tutorial, we learned how to create a simple WordPress plugin to disable the deactivation option for selected plugins. By following the steps outlined above, you can protect essential plugins from accidental deactivation, ensuring the smooth functioning of your WordPress site.