0

I would like to know if there is way to only activate plugins (my own custom plugins) for specific users?

Is there a way to do this using PHP myself and determine which plugin loads based on either user ID or role (or any other factor really).

If this isn't possible, is it possible within the plugin itself to determine which plugin files are loaded again based on the user or role?

I have tried this before in the main plugin file that includes all of my plugin files, but it seems to break the site when I do something like:

if(get_current_user_ID() == 9) {

include_once(dirname(__FILE__)."/MyPluginFiles/Directoryone/Controller.php");

} else {

include_once(dirname(__FILE__)."/MyPluginFilesAlternate/Directoryone/Controller.php");

}

So is there some other way to accomplish this? I wanting to roll out the new plugin, but make it selectable so that people can choose the old experience or the new experience and this would be an easy way to do that I would think.

6
  • You need to ask other Pluggable users, 3rd party dev support questions for plugins and themes are off-topic here and not in this stacks scope
    – Tom J Nowell
    Commented Jun 8, 2022 at 15:31
  • Your user ID is a number not a string. However I'd guess that plugin load time is too early to make that check. Do you have an init hook in your plugin? I'd guess you could test the user and roles there to decide whether to go ahead with the init or not.
    – Rup
    Commented Jun 8, 2022 at 18:40
  • But "new version of site" implies theme though, not plugin?
    – Rup
    Commented Jun 8, 2022 at 18:41
  • @Rup ok, I edited the ID and changed the wording. Doesn't really change the question though. This is a custom plugin that basically IS the site, not just a design change. A theme is just for looks and heavy code functionality shouldn't be built on a theme as far as I know, so, while I corrected it to clarify, it will be a new version of the site on a functional level.
    – CRAIG
    Commented Jun 9, 2022 at 1:12
  • Was just trying to understand the situation. My point about the init hook still stands, and there’s probably an earlier one you can use too if needed.
    – Rup
    Commented Jun 9, 2022 at 6:08

1 Answer 1

2

Before loading plugins WP retrieves the list of active and valid plugins with the function wp_get_active_and_valid_plugins. The array of plugins is retrieved in this line:

$active_plugins = (array) get_option( 'active_plugins', array() );

As you can see, this involves a call to get_option, which has a filter to manipulate the result. So if you could build a filter like this:

add_filter ('option_active_plugins','wpse406546_filter_plugins') {
  // remove the plugin from the array
  }

Note that you cannot do something depending on user at this stage, because the current user has not been authenticated yet (see hook order). Also, you must place this filter in a must use plugin, because it must be loaded before WP starts loading the regular plugins.

Further on in the loading process, at the init hook, the user is known and you can build an action there to load the plugin depending on current user.

Beware that there may be unexpected complications if you use this on plugins that you did not develop yourself, because late loading may make the plugin miss a hook. For instance, if you load a plugin at init and the plugin uses the after_setup_theme hook, that will be too late.

1
  • Thanks @cjbj it looks like according to the WP docs, it isn't really meant for plugin or theme devs, but as you mentioned, since this is my own plugin not for distribution, it is worth trying out.
    – CRAIG
    Commented Jun 9, 2022 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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