-
Notifications
You must be signed in to change notification settings - Fork 1
Re‐Using an Existing Block in Your Project
So you found an existing block you feel will work within the project you're currently working on? Great! You're actively reducing code duplication accross our many, many repos.
When re-using a block, you'll pull it into your project and will likely find you need styling tweaks, maybe some small functionality tweaks. This guide is designed to help you achieve that.
To use a block directly, grab its latest release from the repo, and install the main asset ZIP file as a regular plugin. Activate that plugin and you have access to the block.
If you'd prefer to have access to the full monorepo to have a selection of blocks available, you'll need to follow a few steps:
- Clone monorepo into
/plugins/
- Activate the
WordPress.com Special Projects Blocks Monorepo Autoloader
plugin. - Run npm i and npm run build in whatever block plugin folder(s) you want to work with.
- That block plugin will be autoloaded in the background.
At this point you'll have your chosen blocks available to you in the editor.
On a basic level, if a block is a good fit for your project, you're likely only to be styling it for your project. The preferred method of this is to enqueue a block stylesheet from your theme. This ensures that the block styling within your project stays granular and is enqueued only when needed.
- Re-styling the block: https://developer.wordpress.org/themes/features/block-stylesheets/
- Extending the block: https://developer.wordpress.org/news/2024/08/15/how-to-extend-a-wordpress-block/
It’s important to avoid modifying blocks in the monorepo unless absolutely necessary. In that case please contact an engineering lead to discuss your modification plans. If you’re importing an existing block into your project, and need to modify the blocks functionality, styling, or output, there’s a variety of hooks and filters available to do this.
-
Re-styling the block: https://developer.wordpress.org/themes/features/block-stylesheets/
-
Writing block variations: https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/extending-the-query-loop-block/
-
Extending the block edit and save functions, e.g adding classes, adding new controls, add output wrapper: https://gutenberghub.com/how-to-create-block-visibility-extension/
-
Modifying block output: https://developer.wordpress.org/reference/hooks/render_block/ https://developer.wordpress.org/reference/hooks/render_block_this-name/
-
Modify block.json params: https://developer.wordpress.org/reference/hooks/register_block_type_args/
-
Enqueue/dequeue stylesheets and scripts from block.json: https://developer.wordpress.org/reference/hooks/register_block_type_args/
-
https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/