Using WordPress ‘network_allowed_themes’ PHP filter

network_allowed_themes is a WordPress PHP filter that filters the themes allowed on a network, providing the site context to further filter the list of allowed themes.

Usage

add_filter('network_allowed_themes', 'your_custom_function', 10, 2);

function your_custom_function($allowed_themes, $blog_id) {
    // your custom code here

    return $allowed_themes;
}

Parameters

  • $allowed_themes (string[]): An array of theme stylesheet names.
  • $blog_id (int): ID of the site.

Examples

Limit allowed themes for a specific site

add_filter('network_allowed_themes', 'limit_allowed_themes', 10, 2);

function limit_allowed_themes($allowed_themes, $blog_id) {
    // Only apply the filter for blog ID 2
    if ($blog_id === 2) {
        // Only allow 'twentytwenty' and 'twentytwentyone' themes
        return ['twentytwenty', 'twentytwentyone'];
    }

    return $allowed_themes;
}

This code limits the allowed themes for a specific site (with the ID 2) to only ‘twentytwenty’ and ‘twentytwentyone’ themes.

Allow a custom theme for a specific site

add_filter('network_allowed_themes', 'allow_custom_theme', 10, 2);

function allow_custom_theme($allowed_themes, $blog_id) {
    // Add a custom theme for blog ID 3
    if ($blog_id === 3) {
        $allowed_themes[] = 'my-custom-theme';
    }

    return $allowed_themes;
}

This code allows a custom theme named ‘my-custom-theme’ for a specific site with the ID 3.

Remove a theme from the allowed list

add_filter('network_allowed_themes', 'remove_theme_from_allowed_list', 10, 2);

function remove_theme_from_allowed_list($allowed_themes, $blog_id) {
    // Remove 'twentytwenty' theme from the allowed list
    if (($key = array_search('twentytwenty', $allowed_themes)) !== false) {
        unset($allowed_themes[$key]);
    }

    return $allowed_themes;
}

This code removes the ‘twentytwenty’ theme from the list of allowed themes on all sites.

Allow all themes for a specific site

add_filter('network_allowed_themes', 'allow_all_themes_for_site', 10, 2);

function allow_all_themes_for_site($allowed_themes, $blog_id) {
    // Allow all themes for blog ID 4
    if ($blog_id === 4) {
        $all_themes = wp_get_themes();
        $allowed_themes = array_keys($all_themes);
    }

    return $allowed_themes;
}

This code allows all themes for a specific site with the ID 4.

Prevent any theme from being allowed on a specific site

add_filter('network_allowed_themes', 'prevent_themes_for_site', 10, 2);

function prevent_themes_for_site($allowed_themes, $blog_id) {
    // Prevent any theme from being allowed on blog ID 5
    if ($blog_id === 5) {
        return [];
    }

    return $allowed_themes;
}

This code prevents any theme from being allowed on a specific site with the ID 5.