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 ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think the answer to this Disable plugin / plugin action via theme is good for base knowledge on how to disable plugins from code.
Adapting that knowledge to your needs will leave us with this:
add_action('admin_init', 'my_filter_the_plugins');
function my_filter_the_plugins()
{
global $current_user;
if (in_array('media_manager', $current_user->roles)) {
deactivate_plugins( // deactivate for media_manager
array(
'/advanced-page-manager/advanced_page_manager.php'
),
true, // silent mode (no deactivation hooks fired)
false // network wide
);
} else { // activate for those than can use it
activate_plugins(
array(
'/advanced-page-manager/advanced_page_manager.php'
),
'', // redirect url, does not matter (default is '')
false, // network wise
true // silent mode (no activation hooks fired)
);
}
}
Basically this happens:
For the media_manager user group the my_filter_the_plugins disables(silently) the advanced-page-manager plugin. We then need to reactivate the plugin(silently, again) for those that aren’t in the media_manager user group.
Method 2
You can basically deactivate whole plugin only based on the information you have at wordpress “boot” time, which is basically the URL and cookies. Any check more complex then that will probably have to wait until boot is completed to be able to be executed, at which time the plugin is already loaded and it is too late to “deactivate” it.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0