0

I have installed https://github.com/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.

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

1 Answer 1

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.