函数文档

get_allowed_themes()

💡 云策文档标注

概述

get_allowed_themes() 函数用于获取当前站点允许的主题列表,返回一个以主题名称为键的 WP_Theme 对象数组。该函数自 WordPress 3.4.0 起已被弃用,建议使用 wp_get_themes() 替代。

关键要点

  • 函数返回类型为 WP_Theme[],数组键为主题名称。
  • 自 WordPress 3.4.0 起,该函数被标记为弃用,推荐使用 wp_get_themes( array( 'allowed' => true ) ) 替代。
  • 函数内部调用 wp_get_themes() 并设置 'allowed' => true 参数来获取允许的主题。
  • 相关函数包括 wp_get_themes() 和 _deprecated_function()。

代码示例

function get_allowed_themes() {
    _deprecated_function( __FUNCTION__, '3.4.0', "wp_get_themes( array( 'allowed' => true ) )" );

    $themes = wp_get_themes( array( 'allowed' => true ) );

    $wp_themes = array();
    foreach ( $themes as $theme ) {
        $wp_themes[ $theme->get('Name') ] = $theme;
    }

    return $wp_themes;
}

注意事项

  • 在开发新代码时,应避免使用此弃用函数,改用 wp_get_themes( array( 'allowed' => true ) ) 以确保兼容性。
  • 弃用信息通过 _deprecated_function() 记录,有助于调试和迁移。

📄 原文内容

Get the allowed themes for the current site.

Description

See also

Return

WP_Theme[] Array of WP_Theme objects keyed by their name.

Source

function get_allowed_themes() {
	_deprecated_function( __FUNCTION__, '3.4.0', "wp_get_themes( array( 'allowed' => true ) )" );

	$themes = wp_get_themes( array( 'allowed' => true ) );

	$wp_themes = array();
	foreach ( $themes as $theme ) {
		$wp_themes[ $theme->get('Name') ] = $theme;
	}

	return $wp_themes;
}

Changelog

Version Description
3.4.0 Deprecated. Use wp_get_themes()
3.0.0 Introduced.