Skip to main content
Block attributes provide information about the data stored by a block. Attributes define the structure of your block’s data and how it’s extracted from saved content.

What are Attributes?

Attributes are the structured data needs of a block. They can exist in different forms when serialized, but are declared together under a common interface. For example, rich content, image URLs, background colors, or button titles.

Defining Attributes

Attributes are specified in the attributes field - an object where each key is the attribute name, and the value is the attribute definition.

Accessing Attributes

Attributes are available in both the edit and save functions:

Type Validation

The type indicates the type of data stored by the attribute. A type is required unless an enum is provided. Valid types:
  • null
  • boolean
  • object
  • array
  • string
  • integer
  • number (same as integer)

Enum Validation

An attribute can be restricted to a fixed set of values using enum:

Attribute Sources

Attribute sources define how attribute values are extracted from saved post content.

No Source (Comment Delimiter)

When no source is specified, data is stored in the block’s comment delimiter:
Saved HTML:

Attribute Source

Extract values from HTML element attributes:
Saved content:
Extracted attribute:

Boolean Attributes

For boolean HTML attributes like disabled:
Saved content:
Extracted attribute:

Text Source

Extract inner text from markup (uses textContent):
Saved content:
Extracted attribute:

HTML Source

Extract inner HTML from markup (uses innerHTML):
Saved content:
Extracted attribute:

Query Source

Extract an array of values from multiple elements:
Saved content:
Extracted attribute:

Selectors

The selector can be any valid CSS selector:
If no selector is specified, the source runs against the block’s root node.

Default Values

Provide default values that are used when the attribute isn’t found in content:

Attribute Roles

The role property designates an attribute’s conceptual type:

Content Role

Marks attributes as user-editable content:
Blocks with content role attributes may be enabled for privileged editing in special circumstances like content-only locking.

Local Role

Marks attributes as temporary and non-persistable:
Attributes with local role are ignored by the Block Serializer and never saved to post content.

Updating Attributes

Use setAttributes to update attribute values in the editor:

Save Function Requirements

The block is responsible for ensuring attributes with a source field are saved correctly:
Attributes without a source are automatically saved in the block comment delimiter.

Complete Example

Best Practices

  1. Use appropriate sources: Store data in HTML when possible rather than comment delimiters to reduce data storage.
  2. Provide defaults: Always provide sensible default values for attributes.
  3. Match save and edit: Ensure your save function outputs the correct HTML structure for your attribute sources.
  4. Type carefully: Use the correct type for each attribute to ensure proper validation.
  5. Document attributes: Add comments to explain complex attribute structures.

Deprecated: Meta Source

Meta attribute sources are deprecated. Use EntityProvider and related hook APIs instead.
For working with post meta, see the Create Meta Block guide.