I'm trying to disable a specific plug in (Woocommerce Pay per Post) based on User Roles.
I have three types of User Roles.
- Vendor
- Contributor
- Admin
My goal is IF the user is CONTRIBUTOR, the said plugin (Woocommerce Pay per Post) will be disable.
ELSE (ADMIN & VENDOR), activate plugin.
I've written this code:
// Disable Plugins to specific user
add_filter( 'option_active_plugins', 'disable_logged_in_plugin' );
function disable_logged_in_plugin( $plugins ) {
// The 'option_active_plugins' hook occurs before any user information get generated,
// so we need to require this file early to be able to check for logged in status
require (ABSPATH . WPINC . '/pluggable.php');
// If we are logged in, and NOT an admin...
if ( current_user_can('Contributor') ) {
// Use the plugin folder and main file name here.
// is used here as an example
$plugins_not_needed = array ('/woocommerce-pay-per-post/woocommerce-pay-per-post.php');
foreach ( $plugins_not_needed as $plugin ) {
$key = array_search( $plugin, $plugins );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
}
return $plugins;
}
The problem is it's not working? User assigned to role 'Contributor' still managed to use the said plug in...
Any idea to make it work? Appreciate your help on this.
Here's the screenshot of that plugin folder and its content.


disable_logged_in_pluginis actually executing and that you're using a must-use plugin for this? - Phil F$pluginsarray and return it, that will be saved to the database for everyone, including admins. Are you trying to hide it in the menus? - Chris Haaswp_get_active_and_valid_pluginsand that is a non-filterable call and "un-booting" a plugin isn't a WordPress concept. Instead, I really think you want to limit access to the features of that plugin, right? - Chris Haas