Site icon My WP Tips

How to extend core WordPress blocks with Blocks API

As WordPress continues to evolve with its block-based editing experience, developers are empowered to enhance the usability and flexibility of the CMS through the Block Editor—also known as Gutenberg. One of the most dynamic features of this system is the ability to extend core WordPress blocks using the Blocks API. This allows developers to customize existing blocks provided by WordPress, rather than creating new ones from scratch, making it easier to maintain consistency and reduce redundancy across themes or plugins.

What is the Blocks API?

The Blocks API is the foundation of block development in WordPress. It allows developers to register, modify, and interact with blocks programmatically. By extending core blocks, you can add custom attributes, controls, and styling options that better suit the specific needs of your project or client.

Why Extend Core Blocks?

Extending core blocks provides several advantages:

For example, you may want to add a custom CSS class toggle to the Paragraph block or introduce a new toolbar button in the Image block.

How to Extend a Core Block Using the Blocks API

1. Use the wp.hooks System

The wp.hooks package enables developers to inject custom logic into the WordPress editor. You can add filters or actions to modify block settings, attributes, and behavior.

2. Add Custom Attributes

To extend a core block like core/paragraph, register additional attributes using the blocks.registerBlockType filter:


wp.hooks.addFilter(
  'blocks.registerBlockType',
  'my-plugin/extend-paragraph',
  (settings, name) => {
    if (name === 'core/paragraph') {
      settings.attributes = {
        ...settings.attributes,
        myCustomAttribute: {
          type: 'boolean',
          default: false,
        },
      };
    }
    return settings;
  }
);

3. Modify the Edit Component

Next, use the Editor’s Higher-Order Component (HOC) pattern with wp.compose.createHigherOrderComponent to render additional controls in the Block Inspector or toolbar.

4. Save the Attribute Properly

Use the blocks.getSaveElement filter to include your custom attribute in the block’s output HTML:


wp.hooks.addFilter(
  'blocks.getSaveElement',
  'my-plugin/save-paragraph-attributes',
  (element, blockType, attributes) => {
    if (blockType.name === 'core/paragraph' && attributes.myCustomAttribute) {
      return React.cloneElement(element, {
        ...element.props,
        className: element.props.className + ' my-custom-class',
      });
    }
    return element;
  }
);

Best Practices

Image not found in postmeta

Potential Use Cases

Conclusion

By mastering the ability to extend core WordPress blocks with the Blocks API, developers unlock a higher level of flexibility and customization. Whether it’s adding new control panels, enriching block markup, or integrating third-party services, the API allows complete control over the editing experience, making WordPress a more powerful platform tailored to specific design goals.

FAQ

Q: Can I extend all core blocks in WordPress?

A: Most core blocks can be extended using the Blocks API, but some blocks may have limitations depending on how they are defined internally.

Q: Do I need to use JavaScript or can I use PHP?

A: Extending the block editor primarily involves JavaScript (specifically React), although block registration and server-side rendering may also use PHP.

Q: Will my custom extensions break during WordPress updates?

A: It’s possible, especially if internal APIs or block structures change. Always test your extensions with WordPress updates or align them with stable APIs.

Q: Can I extend third-party blocks as well?

A: Yes, if the block is registered properly, you can target it by name just like with core blocks. Just make sure you understand how it’s implemented to avoid conflicts.

Exit mobile version