@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:
string | Object
required
Block type name (e.g., ‘namespace/block-name’) or block metadata object
Object
required
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
WPBlockType | undefined
The registered block if successful, otherwise
undefinedBlock Parsing
Parse post content into blocks:string
required
The post content to parse (HTML with block comments)
ParseOptions
Extra options for handling block parsing
Array
Array of block objects with
name, attributes, and innerBlocksBlock Serialization
Serialize blocks back to post content:Array
required
Block or array of blocks to serialize
WPBlockSerializationOptions
Serialization options
string
The serialized post content
API Reference
Block Creation
createBlock
Returns a block object given its type and attributes.string
required
Block name (e.g., ‘core/paragraph’)
Object
Block attributes
Array
Nested blocks
Object
Block object with
name, attributes, innerBlocks, and clientIdcloneBlock
Given a block object, returns a copy with optional merged attributes and/or replaced inner blocks.Object
required
Block instance to clone
Object
Attributes to merge with the cloned block
Array
Inner blocks to replace in the clone
Object
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.string
required
Block name
Object | null
Block type object or null if not found
getBlockTypes
Returns all registered blocks.Array
Array of all registered block settings
unregisterBlockType
Unregisters a block.string
required
Block name
WPBlockType | undefined
The previous block value if successfully unregistered
Block Variations
registerBlockVariation
Registers a new block variation for a given block type.string
required
Name of the block (e.g., ‘core/columns’)
WPBlockVariation
required
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.string
required
Name of the block
string
required
Name of the variation to unregister
Block Styles
registerBlockStyle
Registers a new block style for given block types.string | Array
required
Name of block(s) or array of block names (e.g., ‘core/quote’ or [‘core/group’, ‘core/columns’])
Object
required
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.string
required
Name of the block
string
required
Name of the style to unregister
Block Categories
setCategories
Sets the block categories.WPBlockCategory[]
required
Array of category objects with
title and slug propertiesupdateCategory
Updates a category.string
required
Block category slug
WPBlockCategory
required
Object with properties to update (e.g.,
title)Block Collections
registerBlockCollection
Registers a new block collection to group blocks in the same namespace.string
required
The namespace to group blocks (corresponds to block namespace)
Object
required
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.Array | Object
required
Block(s) to transform
string
required
Target block name
Array | null
Array of transformed blocks or null if transformation failed
getBlockTransforms
Returns normal block transforms for a given direction.string
required
Transform direction: ‘to’ or ‘from’
string | Object
Optional block type or name to filter transforms
Array
Array of block transforms for the direction
getPossibleBlockTransformations
Returns block types that the given blocks can be transformed into.Array
required
Array of blocks
Array
Block types that the blocks can be transformed to
Block Validation
validateBlock
Returns validation results for a parsed block.WPBlock
required
Block object to validate
WPBlockType | string
Block type or name (inferred from block if not provided)
[boolean, Array<LoggerItem>]
Tuple with validation status and array of validation issues
Block Support
hasBlockSupport
Returns true if the block defines support for a feature.string | Object
required
Block name or type object
string
required
Feature to test (e.g., ‘align’, ‘html’, ‘anchor’)
boolean
Default value if feature support is not explicitly defined
boolean
Whether the block supports the feature
getBlockSupport
Returns the block support value for a feature.string | Object
required
Block name or type object
string
required
Feature to retrieve
*
Default value to return if not explicitly defined
* | null
Block support value
Block Bindings
registerBlockBindingsSource
Registers a new block bindings source.WPBlockBindingsSource
required
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.string
required
Block bindings source name
Object | null
Block bindings source or null
unregisterBlockBindingsSource
Unregisters a block bindings source.string
required
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.string
The HTML to convert
string
Plain text version
string
Handle content as blocks or inline:
AUTO: Decide based on contentINLINE: Always handle as inline, return stringBLOCKS: Always handle as blocks, return array
Array | string
List of blocks or string depending on mode
Default Blocks
setDefaultBlockName
Assigns the default block name.string
required
Block name to set as default
getDefaultBlockName
Retrieves the default block name.string | null
Default block name
setFreeformContentHandlerName
Assigns the block name for handling non-block content.string
required
Block name for freeform content
Store
The blocks package includes a data store:Object
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