register_block_type( string|WP_Block_Type $name, array $args = array() )

Registers a block type.


Parameters Parameters

$name

(string|WP_Block_Type) (Required) Block type name including namespace, or alternatively 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. Any arguments may be defined, however the ones described below are supported by default.

  • 'render_callback'
    (callable) Callback used to render blocks of this block type.

Default value: array()


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/blocks.php

function register_block_type( $name, $args = array() ) {
	return WP_Block_Type_Registry::get_instance()->register( $name, $args );
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by jmau

    You can pass custom $attributes which can be used both on editor and front-end in render_callback :

    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',
    			],
    		]
    	]
    ] );
    

    Important (tested in 5.0.3) : in case of array attributes you MUST specify items type. Otherwise it would trigger a notice.

  2. Skip to note 2 content
    Contributed by Shah Alom

    Here is an example snippet that I use for one of my own projects (which is – https://mcqacademy.com/) …

    function mcqa_register_block_related_quiz() {
        if ( ! function_exists( 'register_block_type' ) ) {
            // Gutenberg is not active.
            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' );
    
  3. Skip to note 3 content
    Contributed by livemesh

    The arguments to register_block_type() function match the instance vars of WP_Block_Type class, i.e., attributes, render_callback, editor_script, editor_style, script and style.

    
            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',
    
                )
            );

    This is because the register_block_type() function utilizes the name and arguments provided in the function call to create a new instance of WP_Block_Type class and the instance thus created is registered with the global WP_Block_Type_Registry instance.

You must log in before being able to contribute a note or feedback.