Skip to content

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

Closed

Description

@CreativeDive

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.

Activity

  1. added
    [Feature] Widgets ScreenThe block-based screen that replaced widgets.php.
    [Feature] Widgets CustomizerAbility to add and edit blocks in Customize → Widgets.
    [Type] QuestionQuestions about the design or development of the editor.
    on Jul 7, 2021
  2. noisysocks commented on Jul 7, 2021

    @noisysocks
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Widgets CustomizerAbility to add and edit blocks in Customize → Widgets.[Feature] Widgets ScreenThe block-based screen that replaced widgets.php.[Type] QuestionQuestions about the design or development of the editor.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions