Você está visualizando atualmente WooCommerce: Disable A Plugin For Customers

WooCommerce: Disable A Plugin For Customers

  • Autor do post:
  • Categoria do post:Woocommerce
  • Tempo de leitura:4 minutos de leitura

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!

Here’s a new function you’ll learn about today – deactivate_plugins. You can actually completely disable a plugin via code, and the snippet below gives you a practical example.

PHP Snippet: Conditionally Deactivate / Activate Plugin Based on Logged In User Role

Lots to learn here:

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 Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 7 * @donate $9 https://businessbloomer.com/bloomer-armada/ */ 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, ); } }

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete “?>” if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files – if you need more guidance, please take a look at my free video tutorial “Where to Place WooCommerce Customization?”

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance 🙂

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

Follow @rmelogli

Post navigation

Source