函数原型
register_block_type( string|WP_Block_Type $block_type, array $args = array() ): WP_Block_Type|false
函数描述
Registers a block type. The recommended way is to register a block type using the metadata stored in the block.json file.
是否弃用
未弃用
函数参数
-
$block_type
string|WP_Block_Type
Required - 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.jsonfile is located, or a complete WP_Block_Type instance.
In case a WP_Block_Type is provided, the $args parameter will be ignored. -
$args
array
Optional - Array of block type arguments. Accepts any public property of
WP_Block_Type. See WP_Block_Type::__construct() for information on accepted arguments.More Arguments from WP_Block_Type::__construct( … $args )
Array or string of arguments for registering a block type. Any arguments may be defined, however the ones described below are supported by default.
api_versionstringBlock API version.titlestringHuman-readable block type label.categorystring|nullBlock type category classification, used in search interfaces to arrange block types by category.parentstring[]|nullSetting parent lets a block require that it is only available when nested within the specified blocks.ancestorstring[]|nullSetting ancestor makes a block available only inside the specified block types at any position of the ancestor’s block subtree.iconstring|nullBlock type icon.descriptionstringA detailed block type description.keywordsstring[]Additional keywords to produce block type as result in search interfaces.textdomainstring|nullThe translation textdomain.stylesarray[]Alternative block styles.variationsarray[]Block variations.supportsarray|nullSupported features.examplearray|nullStructured data for the block preview.render_callbackcallable|nullBlock type render callback.attributesarray|nullBlock type attributes property schemas.uses_contextstring[]Context values inherited by blocks of this type.provides_contextstring[]|nullContext provided by blocks of this type.editor_script_handlesstring[]Block type editor only script handles.script_handlesstring[]Block type front end and editor script handles.view_script_handlesstring[]Block type front end only script handles.editor_style_handlesstring[]Block type editor only style handles.style_handlesstring[]Block type front end and editor style handles.
Default:
array()
函数返回值
WP_Block_Type|false The registered block type on success, or false on failure.
函数位置
File: wp-includes/blocks.php.
函数源码
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 );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 5.8.0 | First parameter now accepts a path to the block.json file. |
| 5.0.0 | Introduced. |

