I currently use this code in the main plugins file itself. But that plugin is not my own so I would prefer to be able to upgrade it normally without every time adding this code.

if ( defined( 'WP_CLI' ) && WP_CLI ) {

I need to do this because this plugin produces errors and stops wp-cli to execute correctly so I can't just disable the plugin with wp-cli, do my tasks and re-enable it.

Is there a way I can do something like if x then do not load plugin file x from within a mu-plugin?

3 Answers 3


One of the first things WordPress does to load plugins is get the active plugins as saved in the database:

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

Since it uses get_option() we can use the option_active_plugins filter to modify the list of active plugins on the fly.

function wpse_301282_disable_plugin( $active_plugins ) {
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
        $key = array_search( 'gravityforms/gravityforms.php', $active_plugins );

        if ( $key ) {
            unset( $active_plugins[$key] );

    return $active_plugins;
add_filter( 'option_active_plugins', 'wpse_301282_disable_plugin' );

Just replace gravityforms/gravityforms.php with the directory and filename of the plugin you want to disable.

The problem here is that we are trying to affect the loading of plugins, so we can't do that from within a plugin, because it's too late. In the theme would also be too late.

Thankfully WordPress has "Must Use Plugins" these are plugins you can add that are loaded before and separately to regular plugins, and do not appear in the regular plugins list.

All you need to do to add this code to a Must Use Plugin is to create a wp-content/mu-plugins directory (if it doesn't already exist) and create a PHP file (it can be called anything) with that code in it. You don't need a plugin header or anything else.

Now that code will be loaded before all other plugins when WordPress loads. Since our filter is in place, when WordPress gets the list of active plugins to load the plugin you want to disable will be filtered out of that list if WP-CLI is active.

  • Had the same idea but this doesn't work. At least not in my local environment. Maybe get_option() is returning early when the value is saved in transients/similar and this filter is skipped? (Doesn't work for most cases, only in the admin plugin screen it actually got executed)
    – kero
    Commented Apr 19, 2018 at 15:37
  • Hm. Looking at the source and as far as I can tell the only reason the option_ filter wouldn't be applied is if it doesn't have a value and is returning the default. Commented Apr 19, 2018 at 15:43
  • That was actually the case (no plugins active). I shouldn't keep my test environments too clean ..
    – kero
    Commented Apr 19, 2018 at 15:45
  • 1
    I accepted this because in my case its what I needed. Even though in general the --skip-plugin=x option of wp-cli may be a better way to do it. Commented Apr 20, 2018 at 19:10

You can use the skip-plugins option in WP-CLI to not load individual plugins when using WP-CLI.

You can either use it in command like this:

wp user list --skip-plugins=my-plugin

Or you can add this to your wp-cli.yml file:

- my-plugin

See configuration documentation


Just re-naming the plugin dir name will disable it. I do it sometimes to temporarily disable a plugin [linux]:

mv my-plugin-dir renamed-my-plugin-dir
  • 1
    The OP want to skip the plugin just when using WP-CLI, not for any other activity that might happen at the same time Commented Apr 20, 2018 at 6: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.