Ok, this is an unusual snippet today, but it may happen that for performance / security / conflict / conditional / privacy reasons you may need a certain WooCommerce user role to not see / load / use a given plugin.
Let’s think of an example: as an administrator, you wish to use a CRM plugin to sync your order data to an external software. This plugin, however, does not have the ability to exclude Shop Managers from accessing it, and you don’t want to install yet another plugin to define who can access and who can not.
Another case scenario: Shop Managers and Administrators wish to use a live chat plugin, but they want to restrict the live chat visibility to logged in customers only, while logged out customers should not see anything, hidden code included.
There are a million reasons why this could be helpful. So, let’s see how to actually deactivate a plugin (not disable its scripts – but actually deactivate it) with a handy piece of code. Test it and only then – enjoy!

PHP Snippet: Conditionally Deactivate / Activate Plugin Based on Logged In User Role
Lots to learn here:
- the deactivate_plugins function
- the activate_plugins function
- the name of the plugin/s is in the following format: plugin_folder_slug/main_php_file.php
- the wc_current_user_has_role function
In regard to the actual deactivation / activation process, please leave a comment below if it worked for you. I know I had problems related to object cache that I wasn’t able to solve, but if it works for you then we’re in business!
You can test if this works by logging in with the specific user role (Shop Manager in the example below), go to the Plugins page in the WordPress dashboard, and see if the plugin is in fact deactivated.
/**
* @snippet Activate / Deactivate Plugin By Current User Role
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_action( 'init', 'bbloomer_deactivate_plugin_for_shop_managers' );
function bbloomer_deactivate_plugin_for_shop_managers() {
if ( wp_doing_ajax() ) return;
if ( wc_current_user_has_role( 'shop_manager' ) ) {
deactivate_plugins(
array( 'fluid-checkout/fluid-checkout.php' ),
true,
false,
);
} else {
activate_plugins(
array( 'fluid-checkout/fluid-checkout.php' ),
'',
false,
true,
);
}
}
Nice one, it’s just worth to highlight that ‘$silent’ parameter should be ‘true’ to stop activation/deactivation hooks. You don’t want these hooks running all the time.
Thank you!