函数文档

register_block_template()

💡 云策文档标注

概述

register_block_template() 函数用于在 WordPress 中注册一个块模板,允许开发者定义模板的名称、标题、描述、默认内容等属性,以便在站点编辑器中使用。

关键要点

  • 函数接受两个参数:必需的 $template_name(模板名称,格式为 plugin_uri//template_name)和可选的 $args 数组。
  • $args 数组可包含 title、description、content、post_types 和 plugin 等字段,用于定制模板的显示和可用性。
  • 函数返回 WP_Block_Template 对象(成功时)或 WP_Error 对象(失败时),并依赖于 WP_Block_Templates_Registry 类。
  • 此函数在 WordPress 6.7.0 版本中引入,是块模板系统的一部分。

代码示例

function register_block_template( $template_name, $args = array() ) {
    return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
}

📄 原文内容

Register a block template.

Parameters

$template_namestringrequired
Template name in the form of plugin_uri//template_name.
$argsarray|stringoptional

  • title string
    Optional. Title of the template as it will be shown in the Site Editor and other UI elements.
  • description string
    Optional. Description of the template as it will be shown in the Site Editor.
  • content string
    Optional. Default content of the template that will be used when the template is rendered or edited in the editor.
  • post_types string[]
    Optional. Array of post types to which the template should be available.
  • plugin string
    Optional. Slug of the plugin that registers the template.

Default:array()

Return

WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.

Source

function register_block_template( $template_name, $args = array() ) {
	return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
}

Changelog

Version Description
6.7.0 Introduced.