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

问题描述 投票:0回答:1

我正在开发一个主题,我想了解该主题是否支持网络以实现某些功能。

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

如有任何帮助,我们将不胜感激。

php wordpress plugins themes multisite
1个回答
0
投票

迟了 7.5 年的答案怎么样?

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

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

数据库信息

话虽如此,网络激活的插件存储在主 wp_sitemeta 表中,键为“allowedthemes”。

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

它包含每个主题“slug”作为具有标准数字索引键的数组值,但还包含以主题“slug”为键和布尔值“1”作为值的网络激活主题。为什么?我不知道,但它肯定对某个人、某个地方、某个时间点有意义。

数据库序列化元值

数据库中 wp_sitemeta 表中 meta_key“allowedthemes”的示例 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

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

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

更新

我确实找到了对此感兴趣的过滤器。

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

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

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

wp-admin/themes.php 页面上的“allowed_themes”过滤器示例

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

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”过滤器适用于全网络。像上面的示例一样使用上下文/范围,使其对每个站点/每个角色/每个用户有用,并对其进行限制,以便仅在您需要更改允许的主题时/位置应用它。

© www.soinside.com 2019 - 2024. All rights reserved.