From 2ae518c2e00b67362399dd36c4b3d2de79f93781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Thu, 21 Mar 2024 10:41:28 +0100 Subject: [PATCH] add filter to allow extending the list of post content blocks --- docs/reference-guides/filters/block-filters.md | 18 ++++++++++++++++++ .../disable-non-page-content-blocks.js | 5 +++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/reference-guides/filters/block-filters.md b/docs/reference-guides/filters/block-filters.md index ce7eb4d0b2d12d..7fa20249760408 100644 --- a/docs/reference-guides/filters/block-filters.md +++ b/docs/reference-guides/filters/block-filters.md @@ -283,6 +283,24 @@ wp.hooks.addFilter( ``` +### `editor.postContentBlockTypes` + +Used to modify the list of blocks that should be enabled even when used inside a locked template. Any block that saves data to a post should be added here. Examples of this are the post featured image block. Which often gets used in templates but should still allow selecting the image even when the template is locked. + +_Example:_ + +```js +const addExampleBlockToPostContentBlockTypes = ( blockTypes ) => { + return [ ...blockTypes, 'namespace/example' ]; +}; + +wp.hooks.addFilter( + 'editor.postContentBlockTypes', + 'my-plugin/post-content-block-types', + addExampleBlockToPostContentBlockTypes +); +``` + ## Removing Blocks ### Using a deny list diff --git a/packages/editor/src/components/provider/disable-non-page-content-blocks.js b/packages/editor/src/components/provider/disable-non-page-content-blocks.js index fd4722ebe40f4d..95c668970e304d 100644 --- a/packages/editor/src/components/provider/disable-non-page-content-blocks.js +++ b/packages/editor/src/components/provider/disable-non-page-content-blocks.js @@ -4,12 +4,13 @@ import { useSelect, useDispatch } from '@wordpress/data'; import { store as blockEditorStore } from '@wordpress/block-editor'; import { useEffect } from '@wordpress/element'; +import { applyFilters } from '@wordpress/hooks'; -const PAGE_CONTENT_BLOCKS = [ +const PAGE_CONTENT_BLOCKS = applyFilters( 'editor.postContentBlockTypes', [ 'core/post-title', 'core/post-featured-image', 'core/post-content', -]; +] ); function useDisableNonPageContentBlocks() { const contentIds = useSelect( ( select ) => {