Skip to content

Restrict WordPress Plugin Features by User Role

By ali 4 min read

Restrict WordPress plugin features based on the logged-in user's role

Sometimes you may want a WordPress plugin’s features to be available only to specific users or user roles. For example, you might want subscribers to access the website normally while preventing them from using certain plugin features.

WordPress does not support activating or deactivating a plugin separately for each logged-in user. Plugin activation is a site-wide setting, not a per-user setting. Calling deactivate_plugins() when a subscriber logs in will change the plugin’s activation status for the entire website, not just that subscriber.

Instead of deactivating the plugin, you should restrict its functionality using WordPress roles and capabilities. Here are a few ways to do that.

1. Check the Logged-In User’s Role

You can use is_user_logged_in() and wp_get_current_user() to identify the current user and check their role.

For example, the following code checks whether the logged-in user has the subscriber role:

function geekerhub_check_subscriber_role() {
    if ( ! is_user_logged_in() ) {
        return false;
    }

    $current_user = wp_get_current_user();

    return in_array( 'subscriber', (array) $current_user->roles, true );
}

You can use this function to control whether a particular feature should run for subscribers.

For example:

function geekerhub_run_plugin_feature() {
    if ( geekerhub_check_subscriber_role() ) {
        return;
    }

    // Run the feature for other users.
}
add_action( 'init', 'geekerhub_run_plugin_feature' );

This example skips the code inside the function for subscribers. It does not deactivate the plugin itself. You should replace the example logic with the specific plugin functionality you want to restrict.

2. Use WordPress Capabilities Instead of Roles

Another approach is to use current_user_can() to check whether a user has a particular capability.

Capabilities are generally more flexible than checking role names directly. For example, if you want to allow only users who can edit posts to access a feature, you can use the following code:

function geekerhub_restrict_plugin_feature() {
    if ( ! current_user_can( 'edit_posts' ) ) {
        return;
    }

    // Run the plugin feature for users who can edit posts.
}

In this example, the feature runs only for users who have the edit_posts capability. Users without that capability will be excluded.

You can learn more about this function in the official WordPress documentation: current_user_can().

3. Why You Should Not Use deactivate_plugins() for Individual Users

WordPress provides the deactivate_plugins() function to deactivate plugins programmatically. However, this function changes the active plugin list for the website. It does not provide user-specific plugin deactivation.

For example, the following code deactivates the specified plugin site-wide when the condition is met:

deactivate_plugins('example-plugin/example-plugin.php');

This is not suitable for restricting a plugin based on the currently logged-in user. The plugin’s activation state is shared across the website, so deactivating it for one user affects other users too.

Also, deactivating a plugin during a request does not unload its already-included PHP files or undo code that has already run during that request.

For these reasons, use deactivate_plugins() only when you genuinely want to change the website’s plugin activation state. See the official deactivate_plugins() documentation for details.

4. Create a Custom Plugin to Manage Feature Restrictions

If you need to manage multiple plugin-related restrictions, you can create a small custom WordPress plugin to keep the logic organized.

For example, you can place your role and capability checks in one custom plugin and use them wherever you need to control access to specific features. This is easier to maintain than adding the same conditions in multiple theme files.

You can follow our guide on creating a simple plugin to handle plugin activation and deactivation based on user roles. Remember that any per-user restrictions should control plugin functionality rather than changing the site’s global activation state.

You may also find our guide on creating a custom WordPress plugin useful if you want to organize your custom functionality properly.

Conclusion

You cannot deactivate a WordPress plugin for only one logged-in user using deactivate_plugins(), because plugin activation is a site-wide setting. However, you can achieve a similar result by checking the user’s role or capabilities and preventing selected plugin features from running for users who should not access them.

For most projects, using current_user_can() is a flexible approach. If you need more control, put the restrictions in a custom plugin so they are easier to manage and maintain.