register_block_type()

最后更新于:2021-11-27 21:31:36

register_block_type( string|GC_Block_Type$block_type, array$args=array())

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|GC_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.json file is located, or a complete GC_Block_Type instance. In case a GC_Block_Type is provided, the $args parameter will be ignored.

$args

(array) (Optional) Array of block type arguments. Accepts any public property of GC_Block_Type. See GC_Block_Type::__construct() for information on accepted arguments.

Default value: array()

响应

(GC_Block_Type|false) The registered block type on success, or false on failure.

源文件

文件: gc-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 GC_Block_Type_Registry::get_instance()->register( $block_type, $args );
}
register_block_type( 'my_namespace/my_block', [
	'render_callback' => 'render_callback',
	'attributes'      => [
		'some_string' => [
			'default' => 'default string',
			'type'    => 'string'
		],
		'some_array'  => [
			'type'  => 'array',
			'items' => [
				'type' => 'string',
			],
		]
	]
] );
function mcqa_register_block_related_quiz() {
    if ( ! function_exists( 'register_block_type' ) ) {
        // Block editor is not available.
        return;
    }

    register_block_type( 'mcqac/related-quiz', array(
        'editor_script' => 'mcqac-related-quiz-block-script',
        'editor_style'  => 'mcqac-related-quiz-block-editor-style',
        'style'         => 'mcqac-related-quiz-block-frontend-style',
    ) );
}

// Hook: Editor assets.
add_action( 'init', 'mcqa_register_block_related_quiz' );

        register_block_type(
            'my-custom-blocks/calendar',
            array(
                'attributes' => array(
                    'align' => array(
                        'type' => 'string',
                        'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
                    ),
                    'day' => array(
                        'type' => 'integer',
                    ),
                    'month' => array(
                        'type' => 'integer',
                    ),
                    'year' => array(
                        'type' => 'integer',
                    ),
                ),
                'render_callback' => 'render_block_my_custom_blocks_calendar',
                'editor_script'   => 'calendar-editor-js',
                'editor_style'    => 'calendar-editor-css',
                'script'          => 'calendar-frontend-js',
                'style'           => 'calendar-frontend-css',

            )
        );