函数文档

wp_is_block_theme()

💡 云策文档标注

概述

wp_is_block_theme() 函数用于检测当前激活的主题是否为基于块的主题(block-based theme)。它通过检查主题是否定义了 index.html 块模板来实现,返回布尔值。

关键要点

  • 函数返回布尔值,指示当前主题是否为块主题
  • 内部调用 wp_get_theme()->is_block_theme() 进行检测
  • 在主题目录注册前调用会触发 _doing_it_wrong() 警告,并返回 false
  • 建议在 after_theme_setup 钩子之后使用,以确保正确性
  • 函数自 WordPress 5.9.0 版本引入

注意事项

函数名称可能略有误导,它实际检查的是全站编辑(FSE)主题,通过 index.html 模板定义判断。调用时机不当可能导致错误提示。


📄 原文内容

Returns whether the active theme is a block-based theme or not.

Return

bool Whether the active theme is a block-based theme or not.

Source

function wp_is_block_theme() {
	if ( empty( $GLOBALS['wp_theme_directories'] ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before the theme directory is registered.' ), '6.8.0' );
		return false;
	}

	return wp_get_theme()->is_block_theme();
}

Changelog

Version Description
5.9.0 Introduced.

User Contributed Notes