Skip to main content
Filters and hooks are the primary mechanisms for extending and customizing the WordPress Block Editor. They allow you to modify block behavior, editor settings, and rendering output without modifying core code.

Understanding the Hooks System

WordPress uses the @wordpress/hooks package, which implements an event-driven architecture similar to WordPress PHP hooks:

Basic Hook Usage

Namespace Requirement: Unlike PHP hooks, JavaScript hooks require a unique namespace in the format vendor/plugin/function. This helps identify and manage callbacks.

Block Registration Filters

blocks.registerBlockType

Modify block settings during client-side registration:

Server-Side Block Registration

block_type_metadata

Filter raw metadata from block.json before processing:

block_type_metadata_settings

Modify processed settings after metadata parsing:

register_block_type_args

Low-level filter applied right before block registration:

Editor Block Filters

editor.BlockEdit

Modify a block’s edit component to add custom controls:
The editor.BlockEdit filter runs for all blocks and can impact performance. Use conditional rendering with props.isSelected when possible:

editor.BlockListBlock

Modify the block wrapper component:

Block Save Filters

blocks.getSaveElement

Modify the saved block element:

blocks.getSaveContent.extraProps

Add extra props to the save element:
Validation Errors: Modifying blocks.getSaveContent.extraProps can cause block validation errors when editing existing content. For existing content, use the server-side render_block filter instead.

Front-End Rendering Filters

render_block

Modify block output on the front end:

render_block_

Block-specific rendering filter:

Block Attributes Filter

blocks.getBlockAttributes

Modify attributes during block parsing:

Theme.json Filters

Server-Side Theme.json Filters

WordPress provides four filters for different theme.json data layers:

Client-Side Settings Filter

blockEditor.useSetting.before

Modify block settings before rendering:

Block Utility Filters

blocks.getBlockDefaultClassName

Customize the default block class name:

blocks.switchToBlockType.transformedBlock

Filter individual transform results:

Hook Events

hookAdded and hookRemoved

Actions triggered when filters/actions are added or removed:

Common Patterns

Conditional Hook Application

Chaining Multiple Modifications

Using Higher-Order Components

Best Practices

  1. Use unique namespaces: Format as vendor/plugin/function to avoid conflicts
  2. Check block names: Always verify the block type before applying modifications
  3. Mind the priority: Lower numbers run first (default is 10)
  4. Use HTML API: For server-side rendering, prefer WP_HTML_Tag_Processor over regex
  5. Avoid validation errors: Use render_block for existing content modifications
  6. Test thoroughly: Hooks can have unexpected interactions - test edge cases
  7. Document your hooks: Explain why hooks are needed and what they modify
  8. Handle undefined values: Always check if attributes or properties exist before using them

Performance Considerations

  • editor.BlockEdit runs for every block - use conditional rendering
  • Cache expensive computations in filter callbacks
  • Remove hooks when no longer needed using removeFilter/removeAction
  • Avoid synchronous API calls in filter callbacks

Next Steps