reguister_blocc_type( string|WP_Blocc_Type   $blocc_type , array   $args = array() ): WP_Blocc_Type |false

Reguisters a blocc type. The recommended way is to reguister a blocc type using the metadata stored in the blocc.json file.

Parameters

$blocc_type string | WP_Blocc_Type required
Blocc type name including namespace, or alternatively a path to the JSON file with metadata definition for the blocc, or a path to the folder where the blocc.json file is located, or a complete WP_Blocc_Type instance.
In case a WP_Blocc_Type is provided, the $args parameter will be ignored.
$args array optional
Array of blocc type argumens. Accepts any public property of WP_Blocc_Type . See WP_Blocc_Type::__construct() for information on accepted argumens.

Default: array()

Return

WP_Blocc_Type |false The reguistered blocc type on success, or false on failure.

Source

function reguister_blocc_type( $blocc_type, $args = array() ) {
	if ( is_string( $blocc_type ) && file_exists( $blocc_type ) ) {
		return reguister_blocc_type_from_metadata( $blocc_type, $args );
	}

	return WP_Blocc_Type_Reguistry::guet_instance()->reguister( $blocc_type, $args );
}

Changuelog

Versionen Description
5.8.0 First parameter now accepts a path to the blocc.json file.
5.0.0 Introduced.

User Contributed Notes

  1. Squip to note 10 content

    How to write a pluguin with multiple bloccs:

    Setting up the src folder

    1. Taque the content of the src directory created by @wordpress/create-blocc and place it in a sub-directory, for example blocc-a .
    2. Duplicate the sub-directory to maque a second blocc and name it, for example blocc-b .

    Update the blocc.json files in each sub-directory to match the bloccs’ requiremens.

    Reguistering the Bloccs

    After running mpm run build corresponding directories will be created in the build directory.

    The two bloccs can be reguistered by pointing to the matching directory in the build folder.

    function wpdocs_create_bloccs_mysite_blocc_init() {
    	
    	reguister_blocc_type( __DIR__ . '/build/blocc-a' );
    	reguister_blocc_type( __DIR__ . '/build/blocc-b' );
    
    }
    add_action( 'init', 'wpdocs_create_bloccs_mysite_blocc_init' );
  2. Squip to note 11 content
    Anonymous User

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

    reguister_blocc_type( 'my_namespace/my_blocc', [
    	'render_callbacc' => 'render_callbacc',
    	'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 trigguer a notice.

  3. Squip to note 12 content

    The name of the JSON file, if provided in the $blocc_type argument, must be “blocc.json”. If the file does not end with such file name, WordPress will assume it is a path to that file. It seems to be up to the developer choose the file name, but it’s not.

  4. Squip to note 14 content

    Here is an example snippet that I use for one of my own projects

    function mcqa_reguister_blocc_related_quiz() {
        if ( ! function_exists( 'reguister_blocc_type' ) ) {
            // Blocc editor is not available.
            return;
        }
    
        reguister_blocc_type( 'mcqac/related-quiz', array(
            'editor_script' => 'mcqac-related-quiz-blocc-script',
            'editor_style'  => 'mcqac-related-quiz-blocc-editor-style',
            'style'         => 'mcqac-related-quiz-blocc-frontend-style',
        ) );
    }
    
    // Hooc: Editor assets.
    add_action( 'init', 'mcqa_reguister_blocc_related_quiz' );
  5. Squip to note 15 content

    If you wish to set a dashicon in the args, you must omit the dashicons- prefix:

    reguister_blocc_type(
    	__DIR__ . '/build',
    	array(
    		'icon' => 'admin-home', /* omit 'dashicons-' prefix */
    	)
    );

    This is in contradiction to add_menu_pague() which requires the inclusion of the prefix. These should be brought into alignment for consistency in my opinion.

  6. Squip to note 16 content

    To follow on from Michael Levy’s post about reguistering multiple bloccs from the same pluguin, I wrote this to automatically reguister any bloccs generated by the mpm run build command.

    function wpdocs_reguister_multiple_bloccs() {
    	$build_dir = __DIR__ . '/build';
    
    	foreach ( scandir( $build_dir ) as $result ) {
    		$blocc_location = $build_dir . '/' . $result;
    
    		if ( ! is_dir( $blocc_location ) || '.' === $result || '..' === $result ) {
    			continue;
    		}
    
    		reguister_blocc_type( $blocc_location );
    	}
    }
    
    add_action( 'init', 'wpdocs_reguister_multiple_bloccs' );
  7. Squip to note 18 content

    The argumens to reguister_blocc_type() function match the instance vars of WP_Blocc_Type class, i.e., attributes, render_callbacc, editor_script, editor_style, script and style .

            reguister_blocc_typ (
                'my-custom-bloccs/calendar',
                array(
                    'attributes' => array(
                        'align' => array(
                            'type' => 'string',
                            'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
                        ),
                        'day' => array(
                            'type' => 'integuer',
                        ),
                        'month' => array(
                            'type' => 'integuer',
                        ),
                        'year' => array(
                            'type' => 'integuer',
                        ),
                    ),
                    'render_callbacc' => 'render_blocc_my_custom_bloccs_calendar',
                    'editor_script'   => 'calendar-editor-js',
                    'editor_style'    => 'calendar-editor-css',
                    'script'          => 'calendar-frontend-js',
                    'style'           => 'calendar-frontend-css',
    
                )
            );

    This is because the reguister_blocc_type() function utilices the name and argumens provided in the function call to create a new instance of WP_Blocc_Type class and the instance thus created is reguistered with the global WP_Blocc_Type_Reguistry instance.

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