skip to Main Content

I’m trying to deactivate the (TeraWallet) plugin for all user roles only allow the subscriber roles.

I’m using this code to deactivate the plugin for the customer role:

function desactivate_plugin_wallet()
{
    global $current_user;
    if (in_array('customer', $current_user->roles)) {
        deactivate_plugins('/woo-wallet/woo-wallet.php');
    } else {
        activate_plugins('/woo-wallet/woo-wallet.php');
    }
}
add_action('admin_init', 'desactivate_plugin_wallet');

And I applying the code to activate the theme dfunction.php file.

But the code not working.

Advance thank you to a great and senior developer to resolve the problem.

2

Answers


  1. As I am testing from my end, It’s working perfectly, My test code sample is:

    function desactivate_plugin_wallet()
    {
        $myplugin = 'woo-wallet/woo-wallet.php';
    
        if (current_user_can( 'administrator' )) {
            activate_plugins($myplugin);
        } else {
            deactivate_plugins($myplugin);
        }
    }
    add_action('admin_init', 'desactivate_plugin_wallet', 10, 2);
    
    Login or Signup to reply.
  2. add_action('admin_init', 'my_filter_the_plugins');    
        function my_filter_the_plugins()
        {
            global $current_user;
            if (in_array('customer', $current_user->roles)) {
                deactivate_plugins( // deactivate for media_manager
                    array(
                        '/woo-wallet/woo-wallet.php'
                    ),
                    true, // silent mode (no deactivation hooks fired)
                    false // network wide
                );
            } else { // activate for those than can use it
                activate_plugins(
                    array(
                        '/woo-wallet/woo-wallet.php'
                    ),
                    '', // redirect url, does not matter (default is '')
                    false, // network wise
                    true // silent mode (no activation hooks fired)
                );
            }
        }
    

    Basically this happens:

    For the customer user group the my_filter_the_plugins disables(silently) the plugin. We then need to reactivate the plugin(silently, again) for those that aren’t in the customer user group.

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