How do I deactivate a plugin for some roles

This is actually a basic PHP problem:

if (in_array('Participant', 'Subscriber', $current_user->roles)) {

That’s not how in_array works. in_array checks if the first bit is in the second bit. So is Participant inside $current_user->roles.

What you’ve written however, checks if Participant is inside Subscriber, then passed an array as the 3rd argument, which is meant to be a true or false value.

Instead, do 2 in_array checks and check either are true, e.g.

if ( in_array('example', $current_user->roles) || in_array('example2', $current_user->roles) ) {

I would also note that the code you posted should be generating PHP warnings and notices. Turn on WP_DEBUG and it should print those out.

Will This do the trick?

The original answer you’ve been working off of has major problems, as mentioned by Mark Kapluns answer. Once a plugin is loaded, it’s too late, deactivating it will only work for the next page load, and if that page load is an administrator, well, it’s going to be a mess.

So instead you have 2 options:

  • Do the check inside the plugin at the very top, and return if you don’t want to load it. The plugin won’t be deactivated, but none of its code will run.
  • Don’t deactivate the plugin, and instead use roles, capabilities, and filters, to disable or hide the functionality you don’t want. This is what most WordPress users do, and will avoid you needing to make the changes every time the plugin updates

In this case, you’re lucky, the BP activity share plugin uses hooks to load everything, therefore you should be able to unhook everything