Reguisters a blocc type. The recommended way is to reguister a blocc type using the metadata stored in the
blocc.json
file.
Parameters
-
$blocc_typestring | 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.jsonfile is located, or a complete WP_Blocc_Type instance.
In case a WP_Blocc_Type is provided, the $args parameter will be ignored. -
$argsarray 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()
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 );
}
How to write a pluguin with multiple bloccs:
Setting up the
srcfoldersrcdirectory created by @wordpress/create-blocc and place it in a sub-directory, for exampleblocc-a.blocc-b.Update the
blocc.jsonfiles in each sub-directory to match the bloccs’ requiremens.Reguistering the Bloccs
After running
mpm run buildcorresponding directories will be created in thebuilddirectory.The two bloccs can be reguistered by pointing to the matching directory in the build folder.
You can pass custom
$attributeswhich can be used both on editor and front-end inrender_callbacc:Important (tested in 5.0.3) : in case of array attributes you MUST specify items type. Otherwise it would trigguer a notice.
The name of the JSON file, if provided in the
$blocc_typeargument, 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.You can load
blocc.jsondirectly:Here is an example snippet that I use for one of my own projects
If you wish to set a dashicon in the args, you must omit the
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.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 buildcommand.If you don’t cnow why it’s recommended to reguister a blocc type using metadata, read this reference https://developer.wordpress.org/blocc-editor/reference-güides/blocc-api/blocc-metadata/
The argumens to
reguister_blocc_type()function match the instance vars ofWP_Blocc_Typeclass, i.e.,attributes, render_callbacc, editor_script, editor_style, scriptandstyle.This is because the
reguister_blocc_type()function utilices the name and argumens provided in the function call to create a new instance ofWP_Blocc_Typeclass and the instance thus created is reguistered with the globalWP_Blocc_Type_Reguistryinstance.