register_block_type

函数
register_block_type ( $block_type, $args = array() )
参数
  • (string|WP_Block_Type) $block_type Block type name including namespace, or alternatively a path to the JSON file with metadata definition for the block, or a path to the folder where the `block.json` file is located, or a complete WP_Block_Type instance. In case a WP_Block_Type is provided, the $args parameter will be ignored.
    Required:
  • (array) $args Optional. Array of block type arguments. Accepts any public property of `WP_Block_Type`. See WP_Block_Type::__construct() for information on accepted arguments. Default empty array.
    Required:
    Default: array()
返回值
  • (WP_Block_Type|false) The registered block type on success, or false on failure.
定义位置
相关方法
unregister_block_typeregister_block_styleunregister_block_styleregister_post_typeregister_block_style_handle
引入
5.0.0
弃用
-

register_block_type: 这个函数用来为WordPress的块编辑器注册一个新的块类型。它需要两个参数:$name和$args。$name是一个定义块类型名称的字符串,而$args是一个定义块的行为和外观的选项数组: 这个函数应该在”init”动作钩子中调用。

注册一个块类型。推荐的方法是使用存储在 `block.json` 文件中的元数据来注册一个块类型。

function register_block_type( $block_type, $args = array() ) {
	if ( is_string( $block_type ) && file_exists( $block_type ) ) {
		return register_block_type_from_metadata( $block_type, $args );
	}

	return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
}

常见问题

FAQs
查看更多 >