7

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 ?

  • First time the option is called active plugins are loaded (just a require_once) so even if in later calls the plugin is filtered out from options, plugin file is already required, and doesn't exist a way to remove it. So yes, first time is the only that count. The problem is that in WordPress user login is handled quite after plugins are loaded and this is the reason why your code fails. You have 2 options: force user login earlier; or remove the features plugin adds, i.e. I guess plugin adds some hooks, wait until wp_loaded and then remove all of them. Plugin will be active but "muted". – gmazzap Aug 26 '14 at 6:40
  • Thank you for your response. I was hoping to avoid 'unhooking' all features from the plugin (in case they add more in a update), but... You suggest a way to "force login earlier" ? Is that even possible ? – Simon Aug 26 '14 at 20:25
  • Maybe you should place your comment as an answer, you answered it (negatively, but yet). – Simon Aug 27 '14 at 7:36
  • 1
    If I have time today I'll answer properly, showing a way to solve the problem. – gmazzap Aug 27 '14 at 7:39
  • Would love to have your insights. – Simon Aug 27 '14 at 20:55
5

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.

  • Thank you very much. I did not know about the function to deactivate a plugin. Let me test that soon and come back here to answer the question and report results. – Simon Sep 15 '14 at 17:31
  • @Simon I've tested the code on a website that I am working on. Worked for me. – aifrim Sep 16 '14 at 8:07
  • 1
    How did you go with this @Simon? Don't forget to come back and let us know how you went - and accept this answer (using the check mark on the left) if it helped you. At the moment, it is coming up in our 'unanswered questions' list :) – Tim Malone Jul 7 '16 at 6:29
  • @AndyMacaulay-Brook did you read the comments? It activates & deactivates them silently and does not fire activation & deactivation hooks. – aifrim Jul 7 '16 at 10:59
  • Doh - my bad - deleted comment in the interests of not misleading people – Andy Macaulay-Brook Jul 7 '16 at 11:11
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.