-
Notifications
You must be signed in to change notification settings - Fork 0
Block Support
By default, this plugin limits the support for the blocks it adds to the post and page post types. However, it does offer three filters for extending that support.
If you want to disable the default support entirely - which would have the effect of making the blocks in this plugin available to all post types that use the block editor - use the buBlocks.blockSupport.disableDefault filter. The callback hooked to this filter should return true.
For example:
/**
* Add support for the blocks from the BU-Blocks plugin to all post types.
*/
function enableBUBlocksForAllPostTypes() {
return true;
}
wp.hooks.addFilter(
'buBlocks.blockSupport.disableDefault',
'my-plugin-or-theme/blockSupport',
enableBUBlocksForAllPostTypes
);Use the buBlocks.blockSupport.postTypes filter if you want to add support for BU Blocks to another post type.
The argument passed to the callback is an array of the post types that have block support by default.
For example:
/**
* Adds support for blocks from the BU-Blocks plugin to my post type.
*
* @param {array} defaultPostTypes Default post types with BU-Blocks support.
*/
function filterBUBlocksPostTypes( defaultPostTypes ) {
postTypes = defaultPostTypes.concat( [ 'my_post_type' ] );
return postTypes;
}
wp.hooks.addFilter(
'buBlocks.blockSupport.postTypes',
'my-plugin-or-theme/blockSupport',
filterBUBlocksPostTypes
);The buBlocks.blockSupport.blocks filter passes the array of block names as the callback argument. One way this filter can be used is to add support for specific blocks to another post type.
For example:
/**
* Adds support for specific blocks from BU-Blocks to my post type.
*
* @param {array} blocks Default BU Blocks with limited post type support.
*/
function addBlockSupportToPostType( blocks ) {
// Get the current post type.
const currentPostType = wp.data.select( 'core/editor' ).getCurrentPost().type;
// Bail if the current post type is not my post type.
if ( 'my_post_type' !== currentPostType ) {
return blocks;
}
// The list of blocks for which to enable support.
const enableBlocks = [
'editorial/custom-html',
'bu/buniverse',
];
// Return the list of unsupported blocks after filtering out those to enable.
return blocks.filter( function( block ) {
return !enableBlocks.includes( block );
} );
}
wp.hooks.addFilter(
'buBlocks.blockSupport.blocks',
'my-plugin-or-theme/blockSupport',
addBlockSupportToPostType
);