How to Network Deactivate a WordPress Plugin

Duh! Hit “Network Deactivate” in the Network Admin, right? Yeah….

Next time, think twice before you Network Activate a WordPress plugin. It’s not too easy to deactivate it, while keeping it activated on blogs that actually use it. I wrote a little CLI script to activate a plugin of your choice on all blogs in a network:

define( 'ABSPATH', '/path/to/wordpress/' );
require( '/path/to/wp-config.php' );
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
global $wpdb;

$blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs;" );
foreach ( $blogs as $id ) {
    switch_to_blog( $id );
    activate_plugin( 'your-plugin/your-plugin.php' );
    restore_current_blog();
}

printf( "Plugin has been activated on %d blogs.", count( $blogs ) );

You can use it before you network deactivate a plugin, to make sure it’s still activated on all blogs that (might) use it, but newly created ones will not have it active by default. You can then go through all the blogs with that plugin activate, and deactivate on the ones that are not using it, either manually or with a script similar to the above, depending on the plugin.

For example, you can do a check for an option that a plugin uses with get_option and if it’s empty use deactivate_plugins to deactivate it for that blog. The above trick helped me deactivate three plugins, which were barely used in a network.

What about you? How many plugins have you network activated some day, and not really using them on each and every site in the network?

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.