Question

I want to setup a gutenberg screen with one block only. I know I can hide the add block button with CSS with this code:

.block-editor-inserter {
    display: none;
}

How can I hide the button with a hook/filter? Hiding with css is not ideal because the selectors may change in the future.

enter image description here

Was it helpful?

Solution

You can register a template for your CPT and lock that template so users cannot add, move, or delete blocks.

There are several ways to set up the template, but since your CPT is already registered, the easiest might be:

<?php
add_action( 'init', 'wpse_360075_register_block_template' );
function wpse_360075_register_block_template() {
    // Make sure to change "yourcptslug" to your CPT slug
    $post_type_object = get_post_type_object( 'yourcptslug' );
    // Here is the template - change it to whatever single block you like
    $post_type_object->template = array(
        array( 'core/paragraph', array() ),
    );
    // This locks the template
    $post_type_object->template_lock = 'all';
}
?>

As long as you substitute in your actual CPT slug, and whichever block you want added, this will ensure that any new posts in this CPT will have only the template block and won't allow users to remove it or add other blocks. (If you already have existing posts published in this CPT, you would have to delete them as the template only applies to new posts.)

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top