skip to Main Content

I’m trying to disable a specific plug in (Woocommerce Pay per Post) based on User Roles.

I have three types of User Roles.

  1. Vendor
  2. Contributor
  3. 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.

enter image description here

enter image description here

3

Answers


  1. The issue you’re going to run into is the fact that the code which validates a user is "pluggable" and does not execute until after all the plugins are loaded. So if you want to prevent a plugin from loading you need to put it in a must-use plugin. However, the option_active_plugins filter (the one that executes from WordPress to determine if a plugin should be loaded or not) happens BEFORE pluggable functions. Thus, you have no way of knowing who that user is to prevent the plugin from loading.

    In other words

    1. Must Use plugins get loaded
    2. WordPress checks to see which plugins should load with the option_active_plugins filter
    3. (Regular, Non-Must-Use) Plugins that are returned from the option_active_plugins filter get loaded
    4. Pluggable functions (like wp_set_current_user) get loaded
    5. User is validated

    So…

    The first thing you need to do is put your code in a must-use plugin. That’s the good news. The bad news is you’ll need to find a way to validate the user in your own way. It has to be a hack of some sort, you don’t have much choice in the matter. You could try to create a PHP SESSION that stores the user info (or just capability, etc). And you need to sync that with WordPress every time the user changes. Then you must validate that PHP SESSION against the pluggable user functions once they load. A sanity check should be used to make sure they’re the same, if not throw an error.

    Another option as noted by @ChrisHaas is to either contact the plugin author for guidance, or go through that plugin code to see if there are any action/filters you can/want to unset. I realized neither of these two options (or my main answer) is ideal, but because WordPress allows plugins to determine how a user could be validated. You’re stuck in a paradox.

    If I was in your situation I would first try to see if using some combination of removing actions and filters (or hijacking the filters) that plugin is dependent on would be feasible. That would be the path of least resistance if it could be done.

    (Also, for clarity I think you want to say you want to prevent a plugin from loading in certain situations rather than disabling it).

    Login or Signup to reply.
  2. Your code should work, but you can check your logged in user’s role, using

    wp_get_current_user()
    

    Then you can use that role in the below section as parameter of current_user_can(). And it seems a typo, it should be

    contributor 
    

    instead of

    Contributor
    

    So changing this area might help you

    if ( current_user_can('contributor') ) {
    
    Login or Signup to reply.
  3. You can achieve this by deactivating the plugin for that user role.

    function deactivate_wppp_based_on_role() {
        global $current_user;
        if (in_array('contributor', $current_user->roles)) {
            deactivate_plugins( '/woocommerce-pay-per-post/woocommerce-pay-per-post.php' );
        } else { 
            activate_plugins( '/woocommerce-pay-per-post/woocommerce-pay-per-post.php' );
        }
    }
    add_action('admin_init', 'deactivate_wppp_based_on_role');
    

    Or, if you don’t mind using a plugin, you can try the ‘plugin organizer’ plugin which supports deactivating plugins based on user roles. https://wordpress.org/plugins/plugin-organizer/

    Go to the Plugin Organizer settings page and check the box next to each of the roles you want to be able to disable/enable plugins with. Then a separate container will appear on the post edit screen for you to disable/enable plugins with.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search