-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify Documentation for How to Unregister a Single Block with JS #11723
Comments
The
|
@ocean90 It does work in the console. That makes me think that the documentation is incomplete. I'm guessing that simply putting My code was that exact line copy-pasted into a JS file and then enqueued from a plugin. I confirmed that the file was loaded. Looking just now, I had missed that this appears in the console:
It sounds like the "bug" is incomplete documentation then? Having poked through #10951 and a bunch of other linked tickets, I still don't see anything about how to do this. A pointer would be useful. And FWIW, my "expected behavior" for |
@mrwweb I got this to work by enqueuing a script with I'm actually running into a separate issue where I unregister too many and break things! |
@brianpiccione thanks for the reply. A couple things:
add_action( 'enqueue_block_editor_assets', 'mrw_gutenberg_js' );
function mrw_gutenberg_js() {
wp_enqueue_script(
'simple-gutenberg-js',
plugins_url( 'js/gutenberg.js', __FILE__ ),
array( 'wp-blocks' )
);
}
window.onload = function(){
wp.blocks.unregisterBlockType( 'core/verse' );
}; What's the proper way to use |
Unregistering the block when the window loads is not an ideal solution. In theory additional blocks may be able to be added to after the window has loaded. There is another issues I ran into when disabling blocks. When I disabled the In my case I opted to to just remove unsupported blocks from the inserter. const visibleBlocks = [
'core/paragraph',
'core/header',
'core/list',
]
wp.hooks.addFilter('blocks.registerBlockType', 'hideBlocks', (pSettings, pName) => {
if (visibleBlocks.indexOf(pName) === -1) {
return Object.assign({}, pSettings, {
supports: Object.assign({}, pSettings.supports, {inserter: false})
})
}
return pSettings
}) |
Totally makes sense. I just don't know what the right place is to do it! Whatever it is should be included with the documentation.
That's quite troubling... It sounds like it may simply be unsafe to ever unregister blocks if that's always going to be the case!
Seems like a good workaround at least for now. Does that remove it from the "/" inserter options as well? |
Sorry not sure what you mean. It removes it from the editor GUI... But it wouldn't stop the blocks being added programatically or affect any blocks that are already inserted (they can still be edited or deleted). |
@mcshaman: There are two default ways to add a block the "+" buttons and by typing "/" in an empty paragraph block. I'm curious if it hides it from the latter feature as well as the "+". |
Cool... Didn't know that! Yep it is hidden there as well. |
Also FYI it also removes them from the Transform to menu. |
Tested and confirmed using WordPress 5.0.1 and Gutenberg 4.7.0 Add a
Add a
And then activate the plugin, create a new post, and try to add the verse block—the result is that I see the following error in the console and the verse block still works. (Forgive me if my description is a bit oversimplified! I did worry I took the handbook instructions too literally and would be happy to correct it if you spot anything I've done wrong in my testing. 🙂) |
Describe the bug
Having reviewed the handbook, blog posts, and other various tickets such as #4848, I can't find any documented way to remove a single block.
The
wp.blocks.unregisterBlockTypes()
method in the handbook doesn't seem to work andallowed_block_types
only works for whitelisting blocks.Given that I don't want to have to keep my whitelist up to date with new blocks from core or plugins, being able to blacklist blocks seems extremely reasonable.
Expected behavior
Based on what I currently can find, I would expect this to work:
Desktop (please complete the following information):
Win 10, Firefox 63, WP 5.0-beta3
The text was updated successfully, but these errors were encountered: