在WordPress多站点安装中获取启用网络的主题列表



我正在研究一个主题,我想了解该主题是否启用了网络功能。

谁能解释我如何获取启用网络的主题列表? 或者只是知道主题是否启用网络?

任何帮助,不胜感激。

7.5 年太晚的答案怎么样?

我正在编写一个也需要此功能的插件。

不幸的是,我找不到任何明确定义的"支持网络"的函数、钩子或与主题相关的数据库表键。插件在这方面得到了更多的爱。

数据库信息

话虽如此,网络激活的插件存储在主wp_sitemeta表中,键为"允许的主题"。

不幸的是,(再次),它不是一个可以按原样使用的一致数组。

它包含每个主题"slug"

作为具有标准数字索引键的数组值,但也包含以主题"slug"为键和布尔值"1"的网络激活主题。为什么?我不知道,但可以肯定的是,在某个时间点,某个地方,这对某人来说是有意义的。

数据库序列化元值

数据库中wp_sitemeta表中meta_key"允许的主题"的示例meta_value:

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;}

检索值

获取此值取决于要提供的多站点类型和/或插件兼容性。

对于单网络多站点安装

$allowed_themes = get_site_option('allowedthemes');

对于多网络多站点安装

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

结果

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

结果细分

带有[#] => theme_slug的数组值只是已安装/可用的主题。

带有[theme_slug] => 1的数组值是网络激活的主题。

再说一次,为什么要像这样将它们混合在一个数组中?不能告诉你。它就是这样。

使结果有用

现在有很多方法可以只提取网络激活的主题,或者只是使用array_walk功能和其他技术提取已安装/可用的主题。

我在我正在编写的插件中执行此操作的一种(不那么优雅,但更彻底)方法是遍历wp_get_themes()中的所有主题,只要"主题 slug"是关键,请将其附加到数组以供以后使用:

$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

一旦您拥有一系列像这样的 JUST 网络激活主题,您应该能够通过网络激活主题实现您的目标。

我意识到@nitin-yawalkar 可能不再需要这种帮助,但由于这里和其他地方完全缺乏与这个问题相关的答案,我想插话并添加一些东西来帮助引导人们朝着正确的方向前进。

更新

我确实为此找到了一个感兴趣的过滤器。

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

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

它按

原样不是很有帮助,但在适当的上下文/范围内,它非常方便。

示例"allowed_themes"过滤器在 wp-admin/theme.php 页面上

我正在开发的插件的一个方面允许在多站点上按站点设置允许的主题。此过滤器是网络范围的,因此要基于每个站点应用它,您可以执行以下操作:

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;
    
}

"allowed_themes"过滤器适用于整个网络。使用类似于上述示例的上下文/范围使其对每个站点/每个角色/每个用户有用,并对其进行限制,以便仅在需要更改允许的主题时/位置应用它。

最新更新