Plugin Usage Report for the Network
Adds Network Admin > Plugins > Usage, a table of every installed plugin with how many sites activate it and which ones, so you can safely remove unused plugins.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
add_action( 'network_admin_menu', function () {
add_submenu_page( 'plugins.php', __( 'Plugin usage' ), __( 'Usage' ), 'manage_network_plugins', 'wpide-plugin-usage', function () {
if ( ! current_user_can( 'manage_network_plugins' ) ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$usage = get_site_transient( 'wpide_plugin_usage' );
if ( false === $usage ) {
$usage = array();
foreach ( get_sites( array( 'number' => 0, 'fields' => 'ids', 'deleted' => 0 ) ) as $blog_id ) {
foreach ( (array) get_blog_option( $blog_id, 'active_plugins', array() ) as $file ) {
$usage[ $file ][] = (int) $blog_id;
}
}
set_site_transient( 'wpide_plugin_usage', $usage, HOUR_IN_SECONDS );
}
$network = (array) get_site_option( 'active_sitewide_plugins', array() );
echo '<div class="wrap"><h1>' . esc_html__( 'Plugin usage' ) . '</h1>';
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__( 'Plugin' ) . '</th><th>' . esc_html__( 'Sites' ) . '</th><th>' . esc_html__( 'Where' ) . '</th></tr></thead><tbody>';
foreach ( get_plugins() as $file => $data ) {
$sites = isset( $usage[ $file ] ) ? $usage[ $file ] : array();
if ( isset( $network[ $file ] ) ) {
$where = '<strong>' . esc_html__( 'Network active' ) . '</strong>';
$count = esc_html__( 'All' );
} else {
$count = count( $sites );
$links = array();
foreach ( array_slice( $sites, 0, 20 ) as $id ) {
$links[] = '<a href="' . esc_url( get_admin_url( $id, 'plugins.php' ) ) . '">#' . (int) $id . '</a>';
}
$where = $links ? implode( ' ', $links ) . ( count( $sites ) > 20 ? ' …' : '' ) : '<span style="color:#b32d2e">' . esc_html__( 'Unused' ) . '</span>';
}
printf( '<tr><td>%1$s</td><td>%2$s</td><td>%3$s</td></tr>', esc_html( $data['Name'] ), esc_html( (string) $count ), $where ); // phpcs:ignore WordPress.Security.EscapeOutput -- $where is built from escaped parts.
}
echo '</tbody></table></div>';
} );
} );
add_action( 'activated_plugin', function () {
delete_site_transient( 'wpide_plugin_usage' );
} );
add_action( 'deactivated_plugin', function () {
delete_site_transient( 'wpide_plugin_usage' );
} );
Notes
Reads one option per site; on networks with thousands of sites, open it at a quiet time. Results are cached for an hour.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.