I would like to deactivate a plugin for a specific user. I’m using the following code inside a the wp-content/plugin-mu
plugin file:
add_filter( 'option_active_plugins', 'bo_disable_apm_plugin' );
function bo_disable_apm_plugin( $plugins ) {
global $current_user;
// Not use advanced page manager for media manager
if ( is_admin() && in_array( 'media_manager', $current_user->roles ) ) {
$key = array_search( 'advanced-page-manager/advanced_page_manager.php' , $plugins );
if ( false !== $key ) {
unset( $plugins[$key] );
}
}
return $plugins;
}
Of course, it’s not working. I don’t understand the way option_active_plugins
. By dumping data, I find out the code is executed 7 times.
On the first loop, the user is not know, so the condition is not met. The plugin is still activated.
I have added a more complicated code with three conditionnal : unset the plugin if the user is not set, so the plugin is inactivated each time on the first loop. IF the user is set (next loops), check him to set/unset the plugin accordingly. Wasn’t working either.
I didn’t manage to find the right formula, so maybe I’m wrong somewhere and it can’t be done. Each time, the plugin is either activated or deactivated for all users. It looks like the first iteration is the one that counts.
Is there a way to inactivate a plugin for specific user/group ?