WP_Block_Type::__construct( string $block_type, array|string $args = array() )
Constructor.
Contents
Description
Will populate object properties from the provided arguments.
See also
Parameters
-
$block_typestring Required -
Block type name including namespace.
-
$argsarray|string Optional -
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.selectorsarrayCustom CSS selectors for theme.json style generation.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.block_hooksarray[]Block hooks.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()
Source
File: wp-includes/class-wp-block-type.php.
View all references
public function __construct( $block_type, $args = array() ) {
$this->name = $block_type;
$this->set_props( $args );
}
Changelog
| Version | Description |
|---|---|
| 6.4.0 | Added the block_hooks property. |
| 6.3.0 | Added the selectors property. |
| 6.1.0 | Added the editor_script_handles, script_handles, view_script_handles, editor_style_handles, andstyle_handlesproperties. Deprecated theeditor_script,script,view_script,editor_style, andstyle` properties. |
| 6.0.0 | Added the ancestor property. |
| 5.9.0 | Added the view_script property. |
| 5.8.0 | Added the variations property. |
| 5.6.0 | Added the api_version property. |
| 5.5.0 | Added the title, category, parent, icon, description, keywords, textdomain, styles, supports, example, uses_context, and provides_context properties. |
| 5.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
The documentation above suggests that
$args['script']and$args['style']will be loaded for the “front end”, however in fact it is loaded for both the editor and the front end. If you want certain scripts and styles to be loaded in the front end only, you have to independently load those after a conditional check. UnfortunatelyWP_Block_Type::__construct(and soregister_block_type()andregister_block_type_from_metadata()) does not support loading scripts or styles exclusively for the front end.Top ↑
Feedback
Untested, but possibly checking
is_admin()in those methods would work as a constraint for the frontend. — By crstauf —$args['script']works in WP 5.8 if the handle of the script used inwp_register_scriptis passed. Don’t add the source here but register the script first via thewp_enqueue_scriptsfilter. — By Xaver Birsak —Note that the new to 5.9 view_script parameter only loads the script if render_callback is not defined. See /wp-includes/class-wp-block.php:266: