Gutenberg disallow certain custom blocks but keep all core blocks?
I'm writing a function to allow only some custom blocks - essentially I want to register all the blocks, then based on a database table of 'selected' blocks disallow any other custom block.
I can use allowed_block_types
to make an array of allowed blocks, but it wipes all the core ones, is there a way to either get a reliable list of core blocks, or only add a filter for plugin/theme registered blocks? Or possibly, allow all block
categories then my own block category is filtered?
plugins functions block-editor
add a comment |
I'm writing a function to allow only some custom blocks - essentially I want to register all the blocks, then based on a database table of 'selected' blocks disallow any other custom block.
I can use allowed_block_types
to make an array of allowed blocks, but it wipes all the core ones, is there a way to either get a reliable list of core blocks, or only add a filter for plugin/theme registered blocks? Or possibly, allow all block
categories then my own block category is filtered?
plugins functions block-editor
add a comment |
I'm writing a function to allow only some custom blocks - essentially I want to register all the blocks, then based on a database table of 'selected' blocks disallow any other custom block.
I can use allowed_block_types
to make an array of allowed blocks, but it wipes all the core ones, is there a way to either get a reliable list of core blocks, or only add a filter for plugin/theme registered blocks? Or possibly, allow all block
categories then my own block category is filtered?
plugins functions block-editor
I'm writing a function to allow only some custom blocks - essentially I want to register all the blocks, then based on a database table of 'selected' blocks disallow any other custom block.
I can use allowed_block_types
to make an array of allowed blocks, but it wipes all the core ones, is there a way to either get a reliable list of core blocks, or only add a filter for plugin/theme registered blocks? Or possibly, allow all block
categories then my own block category is filtered?
plugins functions block-editor
plugins functions block-editor
asked 4 hours ago
AravonaAravona
348316
348316
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types
filter.
Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true
to enable all blocks. That way you're unable to obtain the list of all blocks registered.
So if you want to disable just one block, then you have to get all blocks bu yourself...
Here's the list of core blocks:
- core/shortcode
- core/image
- core/gallery
- core/heading
- core/quote
- core/embed
- core/list
- core/separator
- core/more
- core/button
- core/pullquote
- core/table
- core/preformatted
- core/code
- core/html
- core/freeform
- core/latest-posts
- core/categories
- core/cover-image
- core/text-columns
- core/verse
- core/video
- core/audio
- core/block
core/paragraph
core-embed/twitter
- core-embed/youtube
- core-embed/facebook
- core-embed/instagram
- core-embed/wordpress
- core-embed/soundcloud
- core-embed/spotify
- core-embed/flickr
- core-embed/vimeo
- core-embed/animoto
- core-embed/cloudup
- core-embed/collegehumor
- core-embed/dailymotion
- core-embed/funnyordie
- core-embed/hulu
- core-embed/imgur
- core-embed/issuu
- core-embed/kickstarter
- core-embed/meetup-com
- core-embed/mixcloud
- core-embed/photobucket
- core-embed/polldaddy
- core-embed/reddit
- core-embed/reverbnation
- core-embed/screencast
- core-embed/scribd
- core-embed/slideshare
- core-embed/smugmug
- core-embed/speaker
- core-embed/ted
- core-embed/tumblr
- core-embed/videopress
- core-embed/wordpress-tv
On the other hand...
It's a lot easier to unregister given block in JS... In there you can use:
wp.blocks.unregisterBlockType( 'core/verse' );
add a comment |
There's a whitelist blocks removal example from the Gutenberg Handbook:
var allowedBlocks = [
'core/paragraph',
'core/image',
'core/html',
'core/freeform'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One might try to modify it to remove blocks that do not start with core
and are not part of allowedExtraBlocks
(untested):
var allowedExtraBlocks = [
'my-plugin/block-example-1',
'my-plugin/block-example-2'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( ! blockType.name.startsWith( 'core' )
&& allowedExtraBlocks.indexOf( blockType.name ) === -1
) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One could adjust this further to match block names that start with core/
or core-embed/
instead of core
to be more precise.
The blacklist example wraps it with:
wp.domReady( function() {
// ...
} );
so this might be needed for the whitelist example too?
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
It would be nice to have a PHP version, maybe one could look further atWP_Block_Type_Registry::get_instance()->get_all_registered()
within theallowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż
– birgire
1 hour ago
1
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326959%2fgutenberg-disallow-certain-custom-blocks-but-keep-all-core-blocks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types
filter.
Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true
to enable all blocks. That way you're unable to obtain the list of all blocks registered.
So if you want to disable just one block, then you have to get all blocks bu yourself...
Here's the list of core blocks:
- core/shortcode
- core/image
- core/gallery
- core/heading
- core/quote
- core/embed
- core/list
- core/separator
- core/more
- core/button
- core/pullquote
- core/table
- core/preformatted
- core/code
- core/html
- core/freeform
- core/latest-posts
- core/categories
- core/cover-image
- core/text-columns
- core/verse
- core/video
- core/audio
- core/block
core/paragraph
core-embed/twitter
- core-embed/youtube
- core-embed/facebook
- core-embed/instagram
- core-embed/wordpress
- core-embed/soundcloud
- core-embed/spotify
- core-embed/flickr
- core-embed/vimeo
- core-embed/animoto
- core-embed/cloudup
- core-embed/collegehumor
- core-embed/dailymotion
- core-embed/funnyordie
- core-embed/hulu
- core-embed/imgur
- core-embed/issuu
- core-embed/kickstarter
- core-embed/meetup-com
- core-embed/mixcloud
- core-embed/photobucket
- core-embed/polldaddy
- core-embed/reddit
- core-embed/reverbnation
- core-embed/screencast
- core-embed/scribd
- core-embed/slideshare
- core-embed/smugmug
- core-embed/speaker
- core-embed/ted
- core-embed/tumblr
- core-embed/videopress
- core-embed/wordpress-tv
On the other hand...
It's a lot easier to unregister given block in JS... In there you can use:
wp.blocks.unregisterBlockType( 'core/verse' );
add a comment |
AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types
filter.
Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true
to enable all blocks. That way you're unable to obtain the list of all blocks registered.
So if you want to disable just one block, then you have to get all blocks bu yourself...
Here's the list of core blocks:
- core/shortcode
- core/image
- core/gallery
- core/heading
- core/quote
- core/embed
- core/list
- core/separator
- core/more
- core/button
- core/pullquote
- core/table
- core/preformatted
- core/code
- core/html
- core/freeform
- core/latest-posts
- core/categories
- core/cover-image
- core/text-columns
- core/verse
- core/video
- core/audio
- core/block
core/paragraph
core-embed/twitter
- core-embed/youtube
- core-embed/facebook
- core-embed/instagram
- core-embed/wordpress
- core-embed/soundcloud
- core-embed/spotify
- core-embed/flickr
- core-embed/vimeo
- core-embed/animoto
- core-embed/cloudup
- core-embed/collegehumor
- core-embed/dailymotion
- core-embed/funnyordie
- core-embed/hulu
- core-embed/imgur
- core-embed/issuu
- core-embed/kickstarter
- core-embed/meetup-com
- core-embed/mixcloud
- core-embed/photobucket
- core-embed/polldaddy
- core-embed/reddit
- core-embed/reverbnation
- core-embed/screencast
- core-embed/scribd
- core-embed/slideshare
- core-embed/smugmug
- core-embed/speaker
- core-embed/ted
- core-embed/tumblr
- core-embed/videopress
- core-embed/wordpress-tv
On the other hand...
It's a lot easier to unregister given block in JS... In there you can use:
wp.blocks.unregisterBlockType( 'core/verse' );
add a comment |
AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types
filter.
Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true
to enable all blocks. That way you're unable to obtain the list of all blocks registered.
So if you want to disable just one block, then you have to get all blocks bu yourself...
Here's the list of core blocks:
- core/shortcode
- core/image
- core/gallery
- core/heading
- core/quote
- core/embed
- core/list
- core/separator
- core/more
- core/button
- core/pullquote
- core/table
- core/preformatted
- core/code
- core/html
- core/freeform
- core/latest-posts
- core/categories
- core/cover-image
- core/text-columns
- core/verse
- core/video
- core/audio
- core/block
core/paragraph
core-embed/twitter
- core-embed/youtube
- core-embed/facebook
- core-embed/instagram
- core-embed/wordpress
- core-embed/soundcloud
- core-embed/spotify
- core-embed/flickr
- core-embed/vimeo
- core-embed/animoto
- core-embed/cloudup
- core-embed/collegehumor
- core-embed/dailymotion
- core-embed/funnyordie
- core-embed/hulu
- core-embed/imgur
- core-embed/issuu
- core-embed/kickstarter
- core-embed/meetup-com
- core-embed/mixcloud
- core-embed/photobucket
- core-embed/polldaddy
- core-embed/reddit
- core-embed/reverbnation
- core-embed/screencast
- core-embed/scribd
- core-embed/slideshare
- core-embed/smugmug
- core-embed/speaker
- core-embed/ted
- core-embed/tumblr
- core-embed/videopress
- core-embed/wordpress-tv
On the other hand...
It's a lot easier to unregister given block in JS... In there you can use:
wp.blocks.unregisterBlockType( 'core/verse' );
AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types
filter.
Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true
to enable all blocks. That way you're unable to obtain the list of all blocks registered.
So if you want to disable just one block, then you have to get all blocks bu yourself...
Here's the list of core blocks:
- core/shortcode
- core/image
- core/gallery
- core/heading
- core/quote
- core/embed
- core/list
- core/separator
- core/more
- core/button
- core/pullquote
- core/table
- core/preformatted
- core/code
- core/html
- core/freeform
- core/latest-posts
- core/categories
- core/cover-image
- core/text-columns
- core/verse
- core/video
- core/audio
- core/block
core/paragraph
core-embed/twitter
- core-embed/youtube
- core-embed/facebook
- core-embed/instagram
- core-embed/wordpress
- core-embed/soundcloud
- core-embed/spotify
- core-embed/flickr
- core-embed/vimeo
- core-embed/animoto
- core-embed/cloudup
- core-embed/collegehumor
- core-embed/dailymotion
- core-embed/funnyordie
- core-embed/hulu
- core-embed/imgur
- core-embed/issuu
- core-embed/kickstarter
- core-embed/meetup-com
- core-embed/mixcloud
- core-embed/photobucket
- core-embed/polldaddy
- core-embed/reddit
- core-embed/reverbnation
- core-embed/screencast
- core-embed/scribd
- core-embed/slideshare
- core-embed/smugmug
- core-embed/speaker
- core-embed/ted
- core-embed/tumblr
- core-embed/videopress
- core-embed/wordpress-tv
On the other hand...
It's a lot easier to unregister given block in JS... In there you can use:
wp.blocks.unregisterBlockType( 'core/verse' );
answered 4 hours ago
Krzysiek DróżdżKrzysiek Dróżdż
15.1k52842
15.1k52842
add a comment |
add a comment |
There's a whitelist blocks removal example from the Gutenberg Handbook:
var allowedBlocks = [
'core/paragraph',
'core/image',
'core/html',
'core/freeform'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One might try to modify it to remove blocks that do not start with core
and are not part of allowedExtraBlocks
(untested):
var allowedExtraBlocks = [
'my-plugin/block-example-1',
'my-plugin/block-example-2'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( ! blockType.name.startsWith( 'core' )
&& allowedExtraBlocks.indexOf( blockType.name ) === -1
) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One could adjust this further to match block names that start with core/
or core-embed/
instead of core
to be more precise.
The blacklist example wraps it with:
wp.domReady( function() {
// ...
} );
so this might be needed for the whitelist example too?
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
It would be nice to have a PHP version, maybe one could look further atWP_Block_Type_Registry::get_instance()->get_all_registered()
within theallowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż
– birgire
1 hour ago
1
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
add a comment |
There's a whitelist blocks removal example from the Gutenberg Handbook:
var allowedBlocks = [
'core/paragraph',
'core/image',
'core/html',
'core/freeform'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One might try to modify it to remove blocks that do not start with core
and are not part of allowedExtraBlocks
(untested):
var allowedExtraBlocks = [
'my-plugin/block-example-1',
'my-plugin/block-example-2'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( ! blockType.name.startsWith( 'core' )
&& allowedExtraBlocks.indexOf( blockType.name ) === -1
) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One could adjust this further to match block names that start with core/
or core-embed/
instead of core
to be more precise.
The blacklist example wraps it with:
wp.domReady( function() {
// ...
} );
so this might be needed for the whitelist example too?
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
It would be nice to have a PHP version, maybe one could look further atWP_Block_Type_Registry::get_instance()->get_all_registered()
within theallowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż
– birgire
1 hour ago
1
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
add a comment |
There's a whitelist blocks removal example from the Gutenberg Handbook:
var allowedBlocks = [
'core/paragraph',
'core/image',
'core/html',
'core/freeform'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One might try to modify it to remove blocks that do not start with core
and are not part of allowedExtraBlocks
(untested):
var allowedExtraBlocks = [
'my-plugin/block-example-1',
'my-plugin/block-example-2'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( ! blockType.name.startsWith( 'core' )
&& allowedExtraBlocks.indexOf( blockType.name ) === -1
) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One could adjust this further to match block names that start with core/
or core-embed/
instead of core
to be more precise.
The blacklist example wraps it with:
wp.domReady( function() {
// ...
} );
so this might be needed for the whitelist example too?
There's a whitelist blocks removal example from the Gutenberg Handbook:
var allowedBlocks = [
'core/paragraph',
'core/image',
'core/html',
'core/freeform'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One might try to modify it to remove blocks that do not start with core
and are not part of allowedExtraBlocks
(untested):
var allowedExtraBlocks = [
'my-plugin/block-example-1',
'my-plugin/block-example-2'
];
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( ! blockType.name.startsWith( 'core' )
&& allowedExtraBlocks.indexOf( blockType.name ) === -1
) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
One could adjust this further to match block names that start with core/
or core-embed/
instead of core
to be more precise.
The blacklist example wraps it with:
wp.domReady( function() {
// ...
} );
so this might be needed for the whitelist example too?
edited 50 mins ago
answered 1 hour ago
birgirebirgire
52.9k461139
52.9k461139
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
It would be nice to have a PHP version, maybe one could look further atWP_Block_Type_Registry::get_instance()->get_all_registered()
within theallowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż
– birgire
1 hour ago
1
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
add a comment |
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
It would be nice to have a PHP version, maybe one could look further atWP_Block_Type_Registry::get_instance()->get_all_registered()
within theallowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż
– birgire
1 hour ago
1
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
The part I really don’t like in this code is that it’s on JS side. It would be nice to be able to restrict this on PHP side... :(
– Krzysiek Dróżdż
1 hour ago
It would be nice to have a PHP version, maybe one could look further at
WP_Block_Type_Registry::get_instance()->get_all_registered()
within the allowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż– birgire
1 hour ago
It would be nice to have a PHP version, maybe one could look further at
WP_Block_Type_Registry::get_instance()->get_all_registered()
within the allowed_block_types
filter callback, but it will probably not show all blocks there. @KrzysiekDróżdż– birgire
1 hour ago
1
1
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
No, it does not. I’ve already created a ticket with such enhancement in trac
– Krzysiek Dróżdż
1 hour ago
add a comment |
Thanks for contributing an answer to WordPress Development Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326959%2fgutenberg-disallow-certain-custom-blocks-but-keep-all-core-blocks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown