WordPress Multisite Script to find all network enabled/disabled plugins
In Multi-site WordPress environment, it is very hard to identify which plugins are network activated and which are individually activated on sub-sites. I encountered this issue recently and decided to script own a solution to this problem. I was looking to remove all the unused plugins (network + sub-site). I found this script handy as this can be placed in a file and placed in root folder of the WordPress installation. Running it will display all the plugins grouped by per sub-site enabled, by network enabled and all safe to delete plugins.
I hope this could help someone 🙂
PHP Script to find all network activated & sub-site activated plugins
<?php
require_once(dirname(__FILE__).'/wp-load.php');
global $wpdb;
$globally_active_plugins = [];
$removable_plugins = [];
$result = $wpdb->get_results("SELECT `blog_id` FROM `wp_blogs`");
if(count($result)){
foreach($result as $r){
$site_id = $r->blog_id;
switch_to_blog($site_id);
$active_plugins = [];
foreach(get_plugins() as $path => $plugin){
if(is_plugin_active($path)){
$active_plugins[] = $path;
if(! in_array($path, $globally_active_plugins))
$globally_active_plugins[] = $path;
}
}
echo '<a href="'.get_site_url().'">'.get_bloginfo('name').' (site #'.get_current_blog_id().')</a>';
echo '<pre>';
print_r($active_plugins);
echo '</pre>';
}
echo 'ALL REQUIRED PLUGINS - ';
echo '<pre>';
print_r($globally_active_plugins);
echo '</pre>';
foreach(get_plugins() as $path => $plugin){
if(! in_array($path, $globally_active_plugins))
$removable_plugins[] = $path;
}
echo 'PLUGINS THAT CAN BE SAFELY REMOVED - ';
echo '<pre>';
print_r($removable_plugins);
echo '</pre>';
}
?>