函数文档

_add_default_theme_supports()

💡 云策文档标注

概述

_add_default_theme_supports() 是一个 WordPress 函数,用于在 'after_setup_theme' 动作触发时为块主题添加默认的主题支持功能。它仅在当前主题是块主题时执行,自动注册多个核心特性,并调整相关过滤器。

关键要点

  • 函数仅在 wp_is_block_theme() 返回 true 时执行,确保只对块主题生效。
  • 自动添加主题支持包括:post-thumbnails、responsive-embeds、editor-styles、html5(针对特定元素)和 automatic-feed-links。
  • 设置过滤器 should_load_separate_core_block_assets 和 should_load_block_assets_on_demand 为 true,优化块资源加载。
  • 通过自定义过滤器移除 Customizer 的 Menus 面板,当主题不支持 menus 和 widgets 时。
  • 相关函数包括 wp_is_block_theme()、add_theme_support()、current_theme_supports() 和 add_filter()。

代码示例

function _add_default_theme_supports() {
	if ( ! wp_is_block_theme() ) {
		return;
	}

	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'editor-styles' );
	/*
	 * Makes block themes support HTML5 by default for the comment block and search form
	 * (which use default template functions) and `` and `` shortcodes.
	 * Other blocks contain their own HTML5 markup.
	 */
	add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
	add_theme_support( 'automatic-feed-links' );

	add_filter( 'should_load_separate_core_block_assets', '__return_true' );
	add_filter( 'should_load_block_assets_on_demand', '__return_true' );

	/*
	 * Remove the Customizer's Menus panel when block theme is active.
	 */
	add_filter(
		'customize_panel_active',
		static function ( $active, WP_Customize_Panel $panel ) {
			if (
				'nav_menus' === $panel->id &&
				! current_theme_supports( 'menus' ) &&
				! current_theme_supports( 'widgets' )
			) {
				$active = false;
			}
			return $active;
		},
		10,
		2
	);
}

📄 原文内容

Adds default theme supports for block themes when the ‘after_setup_theme’ action fires.

Description

See ‘after_setup_theme’.

Source

function _add_default_theme_supports() {
	if ( ! wp_is_block_theme() ) {
		return;
	}

	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'editor-styles' );
	/*
	 * Makes block themes support HTML5 by default for the comment block and search form
	 * (which use default template functions) and `` and `` shortcodes.
	 * Other blocks contain their own HTML5 markup.
	 */
	add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
	add_theme_support( 'automatic-feed-links' );

	add_filter( 'should_load_separate_core_block_assets', '__return_true' );
	add_filter( 'should_load_block_assets_on_demand', '__return_true' );

	/*
	 * Remove the Customizer's Menus panel when block theme is active.
	 */
	add_filter(
		'customize_panel_active',
		static function ( $active, WP_Customize_Panel $panel ) {
			if (
				'nav_menus' === $panel->id &&
				! current_theme_supports( 'menus' ) &&
				! current_theme_supports( 'widgets' )
			) {
				$active = false;
			}
			return $active;
		},
		10,
		2
	);
}

Changelog

Version Description
5.9.0 Introduced.