user interface - How do I hide the UI for specific Gutenberg Blocks?

admin2025-01-07  3

I have found a article about this filter but I am supposed to pass a array of allowed blocks to it. This is what I got so far that ddd is my debugging function from Kint Debugger it reveals that the $allowed_blocks contains a bool true value rather then a array of all blocks I expected. So where do I get a array of all blocks? What a horrible WP API.

add_filter( 'allowed_block_types', __NAMESPACE__ .  '\remove_core_video_blocks' );

function remove_core_video_blocks( $allowed_blocks ) {

    #ddd($allowed_blocks);

    $allowed_blocks = array_diff( $allowed_blocks, [ 'core-embed/youtube' ] );

    return $allowed_blocks;
}

I tried

return array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list'
);

and that disabled the Block UI but the YouTube block I already had in the post continues to work so I guess the filter does what I am looking for I just need a array of all blocks at that point.

I have found a article about this filter but I am supposed to pass a array of allowed blocks to it. This is what I got so far that ddd is my debugging function from Kint Debugger it reveals that the $allowed_blocks contains a bool true value rather then a array of all blocks I expected. So where do I get a array of all blocks? What a horrible WP API.

add_filter( 'allowed_block_types', __NAMESPACE__ .  '\remove_core_video_blocks' );

function remove_core_video_blocks( $allowed_blocks ) {

    #ddd($allowed_blocks);

    $allowed_blocks = array_diff( $allowed_blocks, [ 'core-embed/youtube' ] );

    return $allowed_blocks;
}

I tried

return array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list'
);

and that disabled the Block UI but the YouTube block I already had in the post continues to work so I guess the filter does what I am looking for I just need a array of all blocks at that point.

Share Improve this question edited Aug 22, 2019 at 17:55 NextGenThemes asked Aug 22, 2019 at 15:29 NextGenThemesNextGenThemes 7357 silver badges28 bronze badges 1
  • I think the allowed_block_types filter filters blocks in the appender, not in the content. So if there were blocks in the content they will still be there. There is no way to get the full list of blocks as some of them are added dynamically through JavaScript. You can read more on this github issue. – Alvaro Commented Aug 22, 2019 at 18:40
Add a comment  | 

1 Answer 1

Reset to default 0

Found a JavaScript solution. If I remember correctly there was some talk about temporary solution this or related Github issues so keep that in mind. But I guess this means only server side so for only UI hiding I guess this will be fine.

const wp = window.wp; // when using webpack

wp.data.dispatch( 'core/edit-post' ).hideBlockTypes( [
    'core-embed/youtube',
    'core-embed/vimeo',
    'core-embed/dailymotion',
    'core-embed/collegehumor',
    'core-embed/ted',
] );

Also found some wrong PHP code that I fixed but its still not working correctly as the array does not contain all blocks.

add_filter( 'allowed_block_types', __NAMESPACE__ .  '\remove_core_video_blocks' );

function remove_core_video_blocks( $allowed_blocks ) {

    $guten_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();

    foreach ( $guten_blocks as $key => $value) {
        $registered_blocks[] = $key;
    }

    $allowed_blocks = array_diff(
        $registered_blocks, // NOTE registered blocks does NOT contains all blocks
        array( 'wp-embed/youtube' )
    );

    return $allowed_blocks;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258602a526.html

最新回复(0)