Title: option_active_plugins Filter disabling plugins
Last modified: April 19, 2021

---

# option_active_plugins Filter disabling plugins

 *  Resolved [Philipp Thom](https://wordpress.org/support/users/philipp-thom/)
 * (@philipp-thom)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/)
 * I placed a hooked function into an mu-plugin.
 * When I disable plugins on rest-api calls, those plugins get deactivated in wordpress
   globally also, not only during this specific request.
 * I also noticed multiple calls of `option_active_plugins`. Then I only disabled
   plugins once, but with same result.
 * Once I filter out any plugin from being loaded on specific page it gets deactivated
   globally.
 * Is there any way to overcome this issue?
    Or is this complex due to the way how
   wordpress works?
 * This is how the function logic looks like:
 *     ```
       $plugins_not_needed = array ("plugin-folder-name/plugin.php")
       foreach ( $plugins_not_needed as $plugin ) {
       	$key = array_search( $plugin, $plugins );
               if ( false !== $key ) {
       	   unset( $plugins[ $key ] );
       	}
       }
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14339799)
 * I don’t recommend selectively deactivating plugins. Instead, let them always 
   be active but manage what code executes based on your criteria. For example, 
   to not execute code on API calls, do something like
 *     ```
       if ( ! defined('REST_REQUEST'))
          require_once('/path/to/non-api-code.php');
       ```
   
 *  Thread Starter [Philipp Thom](https://wordpress.org/support/users/philipp-thom/)
 * (@philipp-thom)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14341945)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) Thank you for reply.
   If understand you right, this only works if all the installed plugins use this
   snippet in their code. So this is in theory likely to be the best solution. But
   how can this be achieved in production?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14342312)
 * Yeah, my scheme only works for plugins you’ve written. I’ve misconstrued your
   situation. You’re removing plugins through the “option_active_plugins” filter,
   correct? That seems like it should work, except you don’t seem to be applying
   any conditions to distinguish API requests from others. Have you tried code similar
   to my suggestion for within plugins in your plugin removal code?
 * It’s not unusual for filter hooks to be called more than once per request. It’s
   generally not a problem even if rather redundant. In the cases where it does 
   cause trouble, we can have our callback remove itself after the first pass through
   to prevent further calls during the current request.
 * If for some reason the “option_active_plugins” still causes trouble for you, 
   most plugins modify how WP works through action and filter hooks. You could selectively
   remove key callbacks to disable the plugin without actually preventing it from
   being activated. It might be pretty tedious to seek out all the key callbacks,
   but it’s a more of an expected approach over preventing deactivation to start
   with.
 *  Thread Starter [Philipp Thom](https://wordpress.org/support/users/philipp-thom/)
 * (@philipp-thom)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14342371)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) thank you again for 
   your reply. You point me to a more reliable solution. I previously saw a similar
   post here, but without any satisfying answer.
 * Now this should be also a good record for others getting to the same point.
 * In meantime I found a plugin, that helps selectively deactivate plugins upon 
   specific calls. [https://wordpress.org/plugins/freesoul-deactivate-plugins/](https://wordpress.org/plugins/freesoul-deactivate-plugins/)
 * I plan your suggestion for near future as enhancement.
    -  This reply was modified 5 years, 1 month ago by [Philipp Thom](https://wordpress.org/support/users/philipp-thom/).
    -  This reply was modified 5 years, 1 month ago by [Philipp Thom](https://wordpress.org/support/users/philipp-thom/).
 *  [brentonklassen](https://wordpress.org/support/users/brentonklassen/)
 * (@brentonklassen)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14778262)
 * Hi, I’m experiencing the same issue. I’m using option_active_plugins to selectively
   disable plugins on certain pages, and it’s working on the frontend, but when 
   used in the admin area, the plugins get deactivated permanently and globally.
   Any idea why this is?
 * Thanks!
 *  Thread Starter [Philipp Thom](https://wordpress.org/support/users/philipp-thom/)
 * (@philipp-thom)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14778622)
 * [@brentonklassen](https://wordpress.org/support/users/brentonklassen/) you see
   this behavior, because I suppose WP runs the hook multiple times. When WP checks
   currently active plugins in admin hook as you describe, it would update options
   table of active plugins on every page load.
 * So a solution would be to check which hook is currently running inside your function.
   
   All backend hooks must then be skipped.
 * I wasn’t able to do this.
 * Maybe you can do this and share us your function.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘option_active_plugins Filter disabling plugins’ is closed to new replies.

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [Philipp Thom](https://wordpress.org/support/users/philipp-thom/)
 * Last activity: [4 years, 9 months ago](https://wordpress.org/support/topic/option_active_plugins-filter-disabling-plugins/#post-14778622)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
