@wordpress/blocks
The@wordpress/blocks package provides the foundational Block API for WordPress. It handles block registration, parsing, serialization, and transformations.
Installation
Current Version: 15.13.0This package assumes an ES2015+ environment. Include the polyfill from @wordpress/babel-preset-default if needed.
What are Blocks?
Blocks are the abstract units of markup that compose the content or layout of a webpage. They combine concepts previously achieved with shortcodes, custom HTML, and embed discovery into a single consistent API.Core Concepts
Block Registration
Register a new block type withregisterBlockType:
Block type name (e.g., ‘namespace/block-name’) or block metadata object
Block settings object with the following properties:
title: Human-readable block nameedit: React component for the editorsave: Function returning the saved outputattributes: Block attribute schemacategory: Block category for the inserter
The registered block if successful, otherwise
undefinedBlock Parsing
Parse post content into blocks:The post content to parse (HTML with block comments)
Extra options for handling block parsing
Array of block objects with
name, attributes, and innerBlocksBlock Serialization
Serialize blocks back to post content:Block or array of blocks to serialize
Serialization options
The serialized post content
API Reference
Block Creation
createBlock
Returns a block object given its type and attributes.Block name (e.g., ‘core/paragraph’)
Block attributes
Nested blocks
Block object with
name, attributes, innerBlocks, and clientIdcloneBlock
Given a block object, returns a copy with optional merged attributes and/or replaced inner blocks.Block instance to clone
Attributes to merge with the cloned block
Inner blocks to replace in the clone
A cloned block with merged attributes
Block Types
registerBlockType
Registers a new block provided a unique name and an object defining its behavior.See the Create a Block tutorial for comprehensive block registration guidance.
getBlockType
Returns a registered block type.Block name
Block type object or null if not found
getBlockTypes
Returns all registered blocks.Array of all registered block settings
unregisterBlockType
Unregisters a block.Block name
The previous block value if successfully unregistered
Block Variations
registerBlockVariation
Registers a new block variation for a given block type.Name of the block (e.g., ‘core/columns’)
Object describing the block variation:
name: Unique variation nametitle: Human-readable variation titleattributes: Default attributes for the variationicon: Optional iconscope: Where the variation appears (inserter, block, transform)
See the Block Variations documentation for more details.
unregisterBlockVariation
Unregisters a block variation defined for a given block type.Name of the block
Name of the variation to unregister
Block Styles
registerBlockStyle
Registers a new block style for given block types.Name of block(s) or array of block names (e.g., ‘core/quote’ or [‘core/group’, ‘core/columns’])
Style variation object:
name: CSS class name applied to the blocklabel: Human-readable label shown to users
See the Block Styles documentation for styling guidance.
unregisterBlockStyle
Unregisters a block style for a given block.Name of the block
Name of the style to unregister
Block Categories
setCategories
Sets the block categories.Array of category objects with
title and slug propertiesupdateCategory
Updates a category.Block category slug
Object with properties to update (e.g.,
title)Block Collections
registerBlockCollection
Registers a new block collection to group blocks in the same namespace.The namespace to group blocks (corresponds to block namespace)
Collection settings:
title: Display title in block insertericon: Optional icon to display
Block Transformations
switchToBlockType
Switch one or more blocks into blocks of a new block type.Block(s) to transform
Target block name
Array of transformed blocks or null if transformation failed
getBlockTransforms
Returns normal block transforms for a given direction.Transform direction: ‘to’ or ‘from’
Optional block type or name to filter transforms
Array of block transforms for the direction
getPossibleBlockTransformations
Returns block types that the given blocks can be transformed into.Array of blocks
Block types that the blocks can be transformed to
Block Validation
validateBlock
Returns validation results for a parsed block.Block object to validate
Block type or name (inferred from block if not provided)
Tuple with validation status and array of validation issues
Block Support
hasBlockSupport
Returns true if the block defines support for a feature.Block name or type object
Feature to test (e.g., ‘align’, ‘html’, ‘anchor’)
Default value if feature support is not explicitly defined
Whether the block supports the feature
getBlockSupport
Returns the block support value for a feature.Block name or type object
Feature to retrieve
Default value to return if not explicitly defined
Block support value
Block Bindings
registerBlockBindingsSource
Registers a new block bindings source.Block bindings source configuration:
name: Unique source identifierlabel: Human-readable labelusesContext: Optional array of context values neededgetValues: Function to retrieve bound valuessetValues: Optional function to update valuescanUserEditValue: Optional function to check edit permissions
Introduced in WordPress 6.7.0
getBlockBindingsSource
Returns a registered block bindings source by name.Block bindings source name
Block bindings source or null
unregisterBlockBindingsSource
Unregisters a block bindings source.Block bindings source name to unregister
Utility Functions
getBlockDefaultClassName
Returns the block’s default classname from its name.pasteHandler
Converts HTML string to blocks, stripping everything else.The HTML to convert
Plain text version
Handle content as blocks or inline:
AUTO: Decide based on contentINLINE: Always handle as inline, return stringBLOCKS: Always handle as blocks, return array
List of blocks or string depending on mode
Default Blocks
setDefaultBlockName
Assigns the default block name.Block name to set as default
getDefaultBlockName
Retrieves the default block name.Default block name
setFreeformContentHandlerName
Assigns the block name for handling non-block content.Block name for freeform content
Store
The blocks package includes a data store:Store definition for the blocks namespace. See @wordpress/data for details.
TypeScript
Type definitions are available:Dependencies
The package depends on:@wordpress/data- State management@wordpress/element- React abstraction@wordpress/i18n- Internationalization@wordpress/hooks- Plugin API@wordpress/rich-text- Rich text formatting@wordpress/dom- DOM utilities- And several utility packages
Resources
Create a Block Tutorial
Learn how to create your first block
Block API Reference
Complete block API documentation
GitHub Repository
Source code and issue tracker
What Are Blocks?
Design principles behind blocks