Halloween Sale 15% off Ends in See the plans
PHP

Theme Usage Report for the Network

Adds Network Admin > Themes > Usage, listing every installed theme with the sites that use it (directly or as a parent theme), so unused themes are easy to spot.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

add_action( 'network_admin_menu', function () {
    add_submenu_page( 'themes.php', __( 'Theme usage' ), __( 'Usage' ), 'manage_network_themes', 'wpide-theme-usage', function () {
        if ( ! current_user_can( 'manage_network_themes' ) ) {
            return;
        }

        $usage = get_site_transient( 'wpide_theme_usage' );

        if ( false === $usage ) {
            $usage = array();
            foreach ( get_sites( array( 'number' => 0, 'fields' => 'ids', 'deleted' => 0 ) ) as $blog_id ) {
                $stylesheet = get_blog_option( $blog_id, 'stylesheet' );
                $template   = get_blog_option( $blog_id, 'template' );
                $usage[ $stylesheet ]['direct'][] = (int) $blog_id;
                if ( $template && $template !== $stylesheet ) {
                    $usage[ $template ]['parent'][] = (int) $blog_id;
                }
            }
            set_site_transient( 'wpide_theme_usage', $usage, HOUR_IN_SECONDS );
        }

        $allowed = WP_Theme::get_allowed_on_network();

        echo '<div class="wrap"><h1>' . esc_html__( 'Theme usage' ) . '</h1><table class="widefat striped"><thead><tr><th>' . esc_html__( 'Theme' ) . '</th><th>' . esc_html__( 'Network enabled' ) . '</th><th>' . esc_html__( 'Active on' ) . '</th><th>' . esc_html__( 'Parent of' ) . '</th></tr></thead><tbody>';

        foreach ( wp_get_themes() as $slug => $theme ) {
            $direct = isset( $usage[ $slug ]['direct'] ) ? count( $usage[ $slug ]['direct'] ) : 0;
            $parent = isset( $usage[ $slug ]['parent'] ) ? count( $usage[ $slug ]['parent'] ) : 0;

            printf(
                '<tr><td>%1$s</td><td>%2$s</td><td>%3$s</td><td>%4$s</td></tr>',
                esc_html( $theme->get( 'Name' ) ),
                isset( $allowed[ $slug ] ) ? '✓' : '—',
                $direct || $parent ? esc_html( (string) $direct ) : '<span style="color:#b32d2e">' . esc_html__( 'Unused' ) . '</span>',
                esc_html( (string) $parent )
            );
        }

        echo '</tbody></table></div>';
    } );
} );

add_action( 'switch_theme', function () {
    delete_site_transient( 'wpide_theme_usage' );
} );

Notes

Cached for an hour and refreshed whenever a site switches theme.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.