admin管理员组文章数量:1133689
Background
I've created a custom top level "page section" block, to work with my existing theme. I've like to restrict top level blocks to ONLY that one block. However, I don't want to disable all blocks completely, because I want all blocks available to use as innerBlocks.
Question
How can I restrict top level blocks, without restricting child level blocks?
Background
I've created a custom top level "page section" block, to work with my existing theme. I've like to restrict top level blocks to ONLY that one block. However, I don't want to disable all blocks completely, because I want all blocks available to use as innerBlocks.
Question
How can I restrict top level blocks, without restricting child level blocks?
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Nov 27, 2019 at 17:14 KeldericKelderic 2511 silver badge12 bronze badges2 Answers
Reset to default 2The short answer is you can’t. But you can accomplish this by using a block template that only contains your block and is locked. If your block has an InnerBlocks instance, you can add any of the registered blocks to it.
add_action( 'init', 'insert_template' );
function insert_template() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template =[ [ 'your-custom-block-name'] ];
$post_type_object->template_lock = 'all';
}
You can add a custom script to the admin panel using the admin_enqueue_scripts
action.
Here is an example of how you can restrict existing blocks to only be included inside certain blocks:
const restrictEditorParentBlocks = (settings, name) => {
const TEXT_EDITOR_BLOCKS = [
'core/heading',
'core/image',
'core/list',
'core/paragraph',
'core/shortcode',
'core/embed',
];
if (TEXT_EDITOR_BLOCKS.includes(name)) {
settings.parent = ['example/my-custom-block-that-needs-text-editor-blocks']
}
console.log(settings, name)
return settings
}
wp.hooks.addFilter(
'blocks.registerBlockType',
'your-project-name/restrict-parent-blocks',
restrictEditorParentBlocks
);
This will disable them for the page, but allow them inside the specified parent blocks.
(see settings.parent
assignment)
本文标签: Gutenberg Restrict Top Level BlocksBut Not Child Blocks
版权声明:本文标题:Gutenberg: Restrict Top Level Blocks, But Not Child Blocks 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736787432a1952933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论