• Resolved devonator

    (@devoantor)


    I am trying to disable the use of the Custom HTML Block in the template editor, but allow all the other blocks to be used. Is there anywhere or some filter that I can use to disable the Custom HTML Block only?

    • This topic was modified 1 year, 6 months ago by devonator.
    • This topic was modified 1 year, 6 months ago by devonator.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @devoantor ,

    You can prevent blocks from appearing in the list of available blocks as follows:

    1. Open a post for editing
    2. Click the vertical three dots icon on top right of the editor
    3. Click Preferences (at the bottom)
    4. Click Blocks
    5. Uncheck the Custom HTML block
    • This reply was modified 1 year, 6 months ago by Paulo Pinto.
    Thread Starter devonator

    (@devoantor)

    @psrpinto Thank you for your reply. How can we do this for the templates editor though? I have this option for pages and posts, but not within the template editor

    It doesn’t look like that option is available in the site editor indeed. You could use a plugin like disable-unused-block-editor-blocks or block-visibility, or there might be others.

    Alternatively, it might be possible to do this in JavaScript, through something like the following, but I wasn’t able to make it work myself.

    wp.blocks.unregisterBlockType( 'core/html' );

    It might also be possible to do so in PHP, using the allowed_block_types_all filter, but I haven’t tested it myself.

    Hello @devoantor

    This can be archived through a code snippet, you will have to use the register_block_type_args filter and prevent core/html block from registering.

    The code example below showcases how you can remove a custom html block, you can use the code snippet in your child themes functions.php or in a custom plugin

    Also, I am not sure if this is ideal way

    /**
     * Filters the arguments for registering a block type.
     *
     * @param array  $args       Array of arguments for registering a block type.
     * @param string $block_type Block type name including namespace.
     * @return array Array of arguments for registering a block type.
     */
    function filter_register_block_type_args( array $args, string $block_type ) {
        if( 'core/html' === $block_type ) {
            return array();
        }
    	return $args;
    }
    
    add_filter( 'register_block_type_args', 'filter_register_block_type_args', 10, 2 );

    Disclaimer : Use code snippet at your own risk

    I hope this helps!

    • This reply was modified 1 year, 6 months ago by Milind More.

    Hi, I found that using the register_block_type_args is not a sound way to disable blocks. Returning an empty array to disable blocks can give unexpected results. For example, trying to disable the Classic Block (core/freeform) this way will actually create empty invalid blocks just about everywhere, filling empty space between every other block!

    Sadly, the only alternative I found is the filter allowed_block_types_all documented here but that only allows white-listing blocks, not black-listing / removing certain blocks…

    Milind More

    (@milindmore22)

    Hello @ravanh yes that’s correct we ca you use allowed_block_types_all filter

    Additionally you can check out the Block Governance plugin by WordPress VIP

    Just wanted to note that there is now an article covering this topic on the WordPress Developer Blog.

    Hello @ndiego

    Thank you for sharing, this will be greatly helpful!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How can we disable gutenberg blocks in for WordPress sites ?’ is closed to new replies.