Plugin Active for Logged in Users only
-
Is there a way to setup a plugin that I want active only for logged in users?
-
This article by Terry Hale suggests that you can create a MU Plugin for this kind of functionality. The code looks like this:
/** * Whenever the 'active_plugins' option is loaded * Remove certain plugins from activation for non-logged in users. * * @param Array $plugins * * @return Array $plugins */ 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 ( is_user_logged_in() & ! is_admin() ) { // Use the plugin folder and main file name here. // Bloom is used here as an example $key = array_search( 'bloom/bloom.php' , $plugins ); if ( false !== $key ) { // Remove the plugin reference, based on its key unset( $plugins[ $key ] ); } // You can "deactive" other plugins here as well, // using the code above as a template. } return $plugins; } add_filter( 'option_active_plugins', 'disable_logged_in_plugin' );
You would need to replace the following with your specific plugin:
$key = array_search( 'bloom/bloom.php' , $plugins );
Note that it’s the plugin directory name and the plugin load file, in most cases it will also be the plugin name but not always.
thank you, I will try this. It would also be helpful to know how to make a plugin active for specific users only. Is this possible?
You could use the same code above but inside the
is_user_logged_in()
conditional you could add another conditional checking againstcurrent_user_can( 'edit_users' )
for capabilities or you can grab the current user with$user = wp_get_current_user();
and check against$user->roles
or more specifically$user->user_login
.I tried your initial code you posted and linked to, and it works great – thanks.
To make the plugin active only for a specific user, unfortunately my php isn’t good enough to work this out. I basically want to say:
If user:”John” is logged in then John then …..
(Otherwise….. All other users (logged or logged out) )I’m sure Howdy would be along soon to help you, but since I’m here, I’ll jump in with an answer. Replace the conditional
if
line in the original code with this:// If we are logged in, and NOT getting admin, & logged in user name is John... if ( is_user_logged_in() && ! is_admin() && 'John' == wp_get_current_user()->user_login ) {
Then above the
return
line, replace the closing curly brace}
with this:} else { // place code here that applies to all other users logged in or not, or admin requests }
The code could look something like this, though this code is entirely untested.
/** * Whenever the 'active_plugins' option is loaded * Remove certain plugins from activation for non-logged in users. * * @param Array $plugins * * @return Array $plugins */ 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'); $user = wp_get_current_user(); $acceptable_users = array( // List of username ( login names ) to enable the plugin for. 'john_doe', 'admin', 'user8162', ); // If we don't have a user, they're not logged in and we can return early // If we do have a user and they're not in our list, we can return early if( empty( $user ) || ( ! empty( $user ) && ! in_array( $user->user_login, $acceptable_users ) ) ) { // Use the plugin folder and main file name here. // Bloom is used here as an example $key = array_search( 'bloom/bloom.php' , $plugins ); if ( false !== $key ) { // Remove the plugin reference, based on its key unset( $plugins[ $key ] ); } } return $plugins; } add_filter( 'option_active_plugins', 'disable_logged_in_plugin' );
So if we don’t have a user to work with, disable the plugin. If we do have a user to work with but their login name is not in our acceptable list, disable the plugin. Otherwise, return plugins as normal.
Thanks bcworkz & Howdy_McGee
Both of these worked perfectly.Thankyou!
- The topic ‘Plugin Active for Logged in Users only’ is closed to new replies.