Skip to content

Instantly share code, notes, and snippets.

@teknikqa
Last active June 1, 2021 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teknikqa/91bbbf64c58cf56e3a3703787306822b to your computer and use it in GitHub Desktop.
Save teknikqa/91bbbf64c58cf56e3a3703787306822b to your computer and use it in GitHub Desktop.
Convert a WordPress plugin from a network activated state to a site activated one.
<?php
/**
* Convert a plugin from a network activated state to a site activated one.
*
* On a multisite WordPress installation, there is no simple way to deactivate a
* network activated plugin while leaving it active on one or two sites, unless
* the plugin was activated first on a site and then activated across the
* network. This script will convert the plugin from a network activated state
* to a site activated one. After running this script, we can then individually
* disable the plugin from sites that do not need it.
*
* To run this script,
* wp eval-file disable-plugin.php
*
* @package WPCustomStandAloneFunction
*/
global $wpdb;
// phpcs:ignore
$blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs;" );
// Modify this as needed.
$plugins_to_activate = array(
'events-calendar-pro',
'the-events-calendar',
'gravityforms',
);
foreach ( $blogs as $blog_id_number ) {
switch_to_blog( $blog_id_number );
foreach ( $plugins_to_activate as $plugin_name ) {
activate_plugin( $plugin_name . '/' . $plugin_name . '.php' );
}
restore_current_blog();
}
printf( 'Plugin has been activated on %d blogs.', count( $blogs ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment