函数文档

wp_render_empty_block_template_warning()

💡 云策文档标注

概述

wp_render_empty_block_template_warning() 函数用于渲染空块模板的警告屏幕,向用户提示模板为空并提供编辑链接。

关键要点

  • 函数接受一个必需的 WP_Block_Template 对象参数 $block_template,并返回警告屏幕的 HTML 字符串。
  • 内部会调用 wp_enqueue_style() 来加载 'wp-empty-template-alert' 样式表,确保警告样式正确显示。
  • 使用 sprintf() 和翻译函数 __() 来生成本地化的警告消息,包含模板标题、警告文本和编辑链接。
  • 相关函数包括 get_edit_post_link()、esc_html() 等,用于生成安全且可编辑的链接和转义 HTML。
  • 该函数自 WordPress 6.8.0 版本引入,主要用于 locate_block_template() 等场景。

代码示例

function wp_render_empty_block_template_warning( $block_template ) {
	wp_enqueue_style( 'wp-empty-template-alert' );
	return sprintf(
		/* translators: %1$s: Block template title. %2$s: Empty template warning message. %3$s: Edit template link. %4$s: Edit template button label. */
		'
			%1$s
			%2$s
			
				%4$s
			
		',
		esc_html( $block_template->title ),
		__( 'This page is blank because the template is empty. You can reset or customize it in the Site Editor.' ),
		get_edit_post_link( $block_template->wp_id, 'site-editor' ),
		__( 'Edit template' )
	);
}

📄 原文内容

Renders a warning screen for empty block templates.

Parameters

$block_templateWP_Block_Templaterequired
The block template object.

Return

string The warning screen HTML.

Source

function wp_render_empty_block_template_warning( $block_template ) {
	wp_enqueue_style( 'wp-empty-template-alert' );
	return sprintf(
		/* translators: %1$s: Block template title. %2$s: Empty template warning message. %3$s: Edit template link. %4$s: Edit template button label. */
		'<div id="wp-empty-template-alert">
			<h2>%1$s</h2>
			<p>%2$s</p>
			<a href="%3$s" class="wp-element-button">
				%4$s
			</a>
		</div>',
		esc_html( $block_template->title ),
		__( 'This page is blank because the template is empty. You can reset or customize it in the Site Editor.' ),
		get_edit_post_link( $block_template->wp_id, 'site-editor' ),
		__( 'Edit template' )
	);
}

Changelog

Version Description
6.8.0 Introduced.