Created
January 9, 2025 04:01
-
-
Save benpearson/362f0553cdf7304cae8fd23079e9bc58 to your computer and use it in GitHub Desktop.
WordPress: Filter what blocks are allowed where
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Allowed blocks | |
* | |
* What blocks are allowed where | |
*/ | |
/** | |
* Allowed core and ACF blocks | |
* | |
* See also unregistering of block variations in `modify-core-blocks.js` | |
*/ | |
function dt_allowed_block_types($allowed_block_types, $block_editor_context) | |
{ | |
$post_type = $block_editor_context->post->post_type; | |
if (!$post_type) { | |
return $allowed_block_types; | |
} | |
// Blocks that support template widths | |
$allowed_block_types_all = [ | |
// Core | |
// 'core/button', | |
// 'core/buttons', | |
// 'core/embed', | |
// 'core-embed/youtube', | |
// 'core/heading', | |
// 'core/html', | |
// 'core/paragraph', | |
// 'core/separator', | |
// 'core/spacer', | |
// ACF | |
// 'acf/call-to-action', | |
// 'acf/form', | |
// 'acf/video-with-placeholder-image', | |
]; | |
// Blocks that only support narrow templates | |
$allowed_block_types_narrow = [ | |
// Core | |
// 'core/gallery', | |
// 'core/image', | |
// 'core/list', | |
// 'core/list-item', | |
// 'core/pullquote', | |
// 'core/embed', | |
// 'core/quote', | |
// ACF | |
// 'acf/downloads', | |
// 'acf/team-members', | |
// 'acf/toggle-sections', | |
]; | |
// Blocks that only support wide templates | |
$allowed_block_types_wide = [ | |
// ACF | |
// 'acf/call-to-action', | |
// 'acf/hero', | |
// 'acf/highlight', | |
// 'acf/logos', | |
// 'acf/posts', | |
// 'acf/panels', | |
// 'acf/show-more', | |
]; | |
// Wide templates | |
$post_types_that_support_wide = [ | |
'page', | |
// 'dt_resource', | |
]; | |
if (in_array($post_type, $post_types_that_support_wide)) { | |
if (dt_is_full_width_template($block_editor_context->post->ID)) { | |
$allowed_block_types = array_merge( | |
$allowed_block_types_all, | |
$allowed_block_types_wide | |
); | |
return $allowed_block_types; | |
} | |
} | |
// Narrow templates | |
$allowed_block_types = array_merge( | |
$allowed_block_types_all, | |
$allowed_block_types_narrow | |
); | |
return $allowed_block_types; | |
} | |
// add_filter('allowed_block_types_all', 'dt_allowed_block_types', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment