@wordpress/blocks package provides the foundational API for working with blocks in WordPress. It includes functions for registering block types, managing block attributes, parsing and serializing blocks, and handling block transformations.
Installation
What is a Block?
A block is the abstract term for units of markup that compose the content or layout of a webpage. Blocks combine concepts of shortcodes, custom HTML, and embeds into a single consistent API.Core Functions
registerBlockType
Registers a new block type.blockNameOrMetadata(string|Object) - Block type name or metadatasettings(Object) - Block configuration
WPBlockType | undefined
createBlock
Creates a new block instance.name(string) - Block type nameattributes(Object) - Block attributesinnerBlocks(Array) - Nested blocks
Object - Block object
parse
Parses post content into blocks.content(string) - Post contentoptions(Object) - Parsing options
Array - Block list
serialize
Serializes blocks to post content.blocks(Array) - Blocks to serializeoptions(Object) - Serialization options
string - Post content
Block Registration
registerBlockCollection
Groups blocks in the inserter by namespace.registerBlockStyle
Registers alternative block styles.registerBlockVariation
Registers block variations.Block Utilities
getBlockType
Retrieves a registered block type.getBlockTypes
Returns all registered blocks.hasBlockSupport
Checks if a block supports a feature.Block Bindings
registerBlockBindingsSource
Registers a block bindings source for dynamic attribute values.Block Transforms
getBlockTransforms
Retrieves available block transforms.switchToBlockType
Transforms blocks to another type.Block Categories
setCategories
Defines available block categories.Store
The blocks package includes a data store accessible via@wordpress/data: