How To Disable Plugin or Theme Installation In WordPress

Home » Blog » WordPress Development » How To Disable Plugin or Theme Installation In WordPress

Do you want to disallow the installation of themes and plugins on your WordPress website?

In this article, I will show you a quick and easy way to do so…

I’ll also share with you a way to disallow the deactivation of specific plugins.

How To Disable Plugin and Theme Installation For Clients

If you build WordPress websites for your clients, you might want to disable the installation of new plugins and themes. This will prevent your clients from messing up your configuration if they decide to play around with their WordPress site.

Disabling the installation of new themes and plugins is really easy, and all it takes is adding this line of code to your website’s wp-config.php file:

define('DISALLOW_FILE_MODS',true);

This file will disallow your clients from uploading new themes or plugins.

How To Disable Plugin Deactivation

If you’ve built a custom plugin for your client, then you’ll want to disable the option to deactivate it. The following snippet will remove the deactivate link that shows up under the plugin name:

add_filter('plugin_action_links', 'wpturbo_disallow_plugin_deactivation', 10, 4);

function wpturbo_disallow_plugin_deactivation( $actions, $plugin_file, $plugin_data, $context ) {

	$plugins_to_disallow = array(
		// The plugin that you don't want to allow deactivation for.
		'custom-plugin-name/custom-plugin-name.php',

		// (optional) Name another plugin that your plugin depends on (such as WooCommerce).
		'woocommerce/woocommerce.php'
	);

	if ( array_key_exists('deactivate', $actions) && in_array( $plugin_file, $plugins_to_disallow)){
		unset( $actions['deactivate'] );
	}
	return $actions;
}

The above code adds a filter function to the ‘plugin_action_links’ filter. This filter is responsible for rendering the quick links you see on the plugin page under every plugin. The above function will first check if the current link being filtered is a ‘deactivate’ link. And if it is, it checks if the plugin to which this link belongs is in the array of plugins we want to disallow the deactivation of.

Conclusion

If you are a web developer who builds client sites using WordPress, the above two snippets will come in handy when you want to disallow the activation/deactivation of themes and plugins.

Leave a Reply

Your email address will not be published. Required fields are marked *

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