admin管理员组

文章数量:1278985

I have installed and want to provide only OpenStreetMap based blocks (which I installed additionally) and remove the coblocks/map block.

add_filter( 'allowed_block_types_all', 'myplugin_allowed_block_types', 0 );

function myplugin_allowed_block_types( $allowed_blocks )
{
    $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
    if (!isset($block_types['coblocks/map'])) {
        wp_die('Block not found to unset');
    }
    unset($block_types['coblocks/map']);
    return array_keys($block_types);
}

This dies with Block not found to unset

Actually the code works without that check, but I am wondering if there are other blocks which are not in the registry but still work and would be disabled with my code.

So essentially I am asking why get_all_registered does not have some blocks which are still there and how I can remove only specific blocks if I can not reliably fetch a list of all blocks.

I have installed https://github/godaddy-wordpress/coblocks and want to provide only OpenStreetMap based blocks (which I installed additionally) and remove the coblocks/map block.

add_filter( 'allowed_block_types_all', 'myplugin_allowed_block_types', 0 );

function myplugin_allowed_block_types( $allowed_blocks )
{
    $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
    if (!isset($block_types['coblocks/map'])) {
        wp_die('Block not found to unset');
    }
    unset($block_types['coblocks/map']);
    return array_keys($block_types);
}

This dies with Block not found to unset

Actually the code works without that check, but I am wondering if there are other blocks which are not in the registry but still work and would be disabled with my code.

So essentially I am asking why get_all_registered does not have some blocks which are still there and how I can remove only specific blocks if I can not reliably fetch a list of all blocks.

Share Improve this question asked Oct 12, 2021 at 15:11 AlexAlex 1471 silver badge7 bronze badges 4
  • 1 Doing this via PHP isn't the most reliable due to the poor API design of that filter. You're better off using a filter in JS to modify the block so that inserter is set to false in its supports section – Tom J Nowell Commented Oct 12, 2021 at 15:27
  • 1 you'll also want to avoid wp_die, this might kill other block based areas or post types e.g. the widget block areas – Tom J Nowell Commented Oct 12, 2021 at 15:28
  • Thanks, the wp_die was for illustration :) how do create a JS filter? Feel free to post as answer – Alex Commented Oct 13, 2021 at 13:03
  • 1 I don't have code to share, but hooks in the editor are well documented in the official handbook – Tom J Nowell Commented Oct 13, 2021 at 14:09
Add a comment  | 

1 Answer 1

Reset to default 2

You'll want to target the block specifically via javascript. Brief example:

wp.domReady(function() {
    wp.blocks.unregisterBlockType( 'coblocks/map' );
});

Save that to a js file and enqueue it via your custom function: add_action('enqueue_block_editor_assets', 'your_custom_enqueue_fucntion')

本文标签: Disable only one Gutenberg block programaticallycoblockmaps not listed in blocks