get list of network enabled themes in wordpress multisite installation

I am working on a theme, where I want to get to know if the theme is network enabled or not for some functionality.

Can anyone explain me how can I get the list of network enabled themes? or just to know a theme is network enabled or not?

Read More

Any help is appreciated.

Related posts

Leave a Reply

1 comment

  1. How about a 7.5 year too late answer?

    I’m writing a plugin that needed this functionality too.

    Unfortunately I couldn’t find any clearly defined “network-enabled” functions, hooks, or DB table keys related to themes. Plugins get a little bit more love in that regard.

    DB Info

    With that said, network-activated plugins are stored in the main wp_sitemeta table with the key “allowedthemes”.

    Unfortunately, (yet again), it’s not a consistent array ready for use as-is.

    It contains EVERY theme “slug” as an array value with standard number index keys, but ALSO contains the network-activated themes with the theme “slug” as the key and a boolean “1” as the value. Why? I have no idea, but surely it made sense to someone, somewhere, at some point in time.

    DB Serialized Meta Value

    Example meta_value for meta_key “allowedthemes” in wp_sitemeta table in the DB:

    a:8:{i:0;s:12:"twentytwenty";i:1;s:15:"twentytwentyone";i:2;s:17:"twentytwentythree";i:3;s:15:"twentytwentytwo";s:12:"twentytwenty";b:1;s:15:"twentytwentyone";b:1;s:17:"twentytwentythree";b:1;s:15:"twentytwentytwo";b:1;}
    

    Retrieving Values

    Getting this value depends on the type of multisite, and/or plugin compatibility you want to offer.

    For single network multisite installs

    $allowed_themes = get_site_option('allowedthemes');
    

    For multinetwork multisite installs

    $allowed_themes = get_network_option(get_current_network_id(), 'allowedthemes');
    

    Results

    echo '<pre>',print_r($allowed_themes),'</pre>';
    
    // shows
    Array
    (
        [0] => twentytwenty
        [1] => twentytwentyone
        [2] => twentytwentythree
        [3] => twentytwentytwo
        [twentytwenty] => 1
        [twentytwentyone] => 1
        [twentytwentythree] => 1
        [twentytwentytwo] => 1
    )
    1
    

    Results Breakdown

    Array values with [#] => theme_slug are just installed/available themes.

    Array values with [theme_slug] => 1 are network-activated themes.

    Again, WHY mix them in one array like this? Couldn’t tell you. It is what it is.

    Making Results Useful

    Now there’s plenty of ways to extract JUST the network activated themes, or JUST the installed/available themes with array_walk functions and other techniques.

    One (less elegant, but more thorough) way I do this within the plugin I’m writing is to loop through all the themes from wp_get_themes() and wherever a “theme slug” is the key, append it to an array for later use:

    $all_themes = wp_get_themes();
    $allowed_themes = get_site_option('allowedthemes');
    
    foreach ($all_themes as $theme => $object) {
        if (isset($allowed_themes[$theme]) && (bool) $allowed_themes[$theme]) {
            $network_activated_themes[] = $theme;
        }
    }
    echo '<pre>',print_r($network_activated_themes),'</pre>';
    
    // shows
    Array
    (
        [0] => twentytwenty
        [1] => twentytwentyone
        [2] => twentytwentythree
        [3] => twentytwentytwo
    )
    1
    
    
    /* ALTERNATE LOOP TO GET THEME OBJECT DATA */
    foreach ($all_themes as $theme => $object) {
        if (isset($allowed_themes[$theme]) && (bool) $allowed_themes[$theme]) {
            $network_activated_themes[$theme] = $object;
        }
    }
    echo '<pre>',print_r($network_activated_themes),'</pre>';
    
    // shows an array identical to wp_get_themes(), but only with network-activated themes
    

    Once you have an array of JUST network-activated themes like this, you should be able to accomplish your goals with network-activated themes.

    I realize that @nitin-yawalkar probably doesn’t need this help anymore, but due to the complete lack of answers here and elsewhere related to this question, I wanted to chime in and add SOMETHING to help steer people in the right direction.

    UPDATE

    I did find a filter of interest for this.

    apply_filters( 'allowed_themes', string[] $allowed_themes )
    

    https://developer.wordpress.org/reference/hooks/allowed_themes/

    It’s not very helpful as-is, but within proper context/scope, it’s quite handy.

    Example ‘allowed_themes’ Filter on wp-admin/themes.php page

    One aspect of the plugin I’m developing allows setting allowed themes on a per-site basis on multisites. This filter is network-wide, so to apply it on a per-site basis you can do something like this:

    add_filter('allowed_themes', 'tqc_show_allowed_themes');
    function tqc_show_allowed_themes( $allowed_themes ) {
        
        // make sure we're in the admin panel
        if ( is_admin() ) {
            
            // make sure we're on the themes.php page
            global $pagenow;
            if ( $pagenow === 'themes.php'  ) {
                
                //print the standard array
                echo '<pre>',print_r( $allowed_themes ),'</pre>';
                
                /*
                 * shows same array as
                 * get_site_option( 'allowedthemes' );
                 * and
                 * get_network_option( get_current_network_id(), 'allowedthemes' );
                 * 
                Array
                (
                    [0] => twentytwenty
                    [1] => twentytwentyone
                    [2] => twentytwentythree
                    [3] => twentytwentytwo
                    [twentytwenty] => 1
                    [twentytwentyone] => 1
                    [twentytwentythree] => 1
                    [twentytwentytwo] => 1
                )
                1
                 * 
                 */
    
                // a separate custom function that retrieves valid themes 
                // allowed to be used by the current site
                $valid_themes = tqc_get_valid_themes( get_current_blog_id() );
                
                // set allowed themes to empty
                $allowed_themes = array();
                
                // loop through once to build out the [#] => theme_slug part
                foreach( $valid_themes as $theme => $values ) {
                    $allowed_themes[] = $theme;
                }
                
                // loop through again to build out the [theme_slug] => 1 part
                foreach( $valid_themes as $theme => $values ) {
                    $allowed_themes[ $theme ] = (bool) true;
                }
                
            }
            
        }
        
        // print the new array
        echo '<pre>',print_r( $allowed_themes ),'</pre>';
        
        /*
         * shows modified array of allowed themes for this specific site
         * 
        Array
        (
            [0] => twentytwenty
            [1] => twentytwentyone
            [twentytwenty] => 1
            [twentytwentyone] => 1
        )
        1
         * 
         */
        
        // return array to the filter to be applied
        return $allowed_themes;
        
    }
    

    The ‘allowed_themes’ filter applies network-wide. Use context/scope like the example above to make it useful per-site / per-role / per-user, and limit it so that it’s only applied when / where you need to alter the allowed themes.