Skip to content
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

How we can allow only specific blocks for the new Widget Screen? #33238

Closed
CreativeDive opened this issue Jul 6, 2021 · 1 comment
Closed
Labels
[Feature] Widgets Customizer Ability to add and edit blocks in Customize → Widgets. [Feature] Widgets Screen The block-based screen that replaced widgets.php. [Type] Question Questions about the design or development of the editor.

Comments

@CreativeDive
Copy link
Contributor

Hey together,

the new widget screen will come to WordPress with the next release. Therefore it's very useful to disallow or allow specific block types only for using on a small width sidebar.

How we can deal with this?

Is there a filter like add_filter( 'allowed_block_types', '...') to allow specific block types for the widget screen?

Thank you for helping.

@talldan talldan added [Feature] Widgets Screen The block-based screen that replaced widgets.php. [Feature] Widgets Customizer Ability to add and edit blocks in Customize → Widgets. [Type] Question Questions about the design or development of the editor. labels Jul 7, 2021
@noisysocks
Copy link
Member

noisysocks commented Jul 7, 2021

Hey @CreativeDive.

You can tell if the block editor is not the post block editor using something like this:

add_filter( 'allowed_block_types_all', function( $allowed_block_types, $block_editor_context ) {
	if ( empty( $block_editor_context->post ) ) {
		// This isn't a post block editor.
	}
	return $allowed_block_types;
} );

To tell if the block editor is the widgets block editor you could use something like this:

add_filter( 'allowed_block_types_all', function( $allowed_block_types, $block_editor_context ) {
	global $pagenow;
	if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
		// This is a widgets block editor.
	}
	return $allowed_block_types;
} );

The latter example is a little hacky. We will look at improving this in 5.8.x, see #28517.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Widgets Customizer Ability to add and edit blocks in Customize → Widgets. [Feature] Widgets Screen The block-based screen that replaced widgets.php. [Type] Question Questions about the design or development of the editor.
Projects
None yet
Development

No branches or pull requests

3 participants