How to specify which Gutenberg blocks are available in the editor for a page template

Using an example given here: enter link description here I’ve successfully been able to restrict the block types available globally or by post type (eg: page). But what I really would like to do is restrict the the block types by page template.
I have tried using if ( is_page_template('page-home.php') ) {...} but with no joy. It will be apparant from the code below, but I am using ACF custom blocks. The blocks themselves are working fine.
So currently I have the following in my functions.php file:

function my_allowed_block_types( $allowed_blocks, $post ) {
// works on all content
$allowed_blocks = array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list'
);
// works for all pages but too general for what I want to do
if( $post->post_type === 'page' ) {
    $allowed_blocks= array( 
        'acf/homepage'
    );
}
// Intended to restrict to just one block on my homepage template but does not
if ( is_page_template('page-home.php') ) {
    $allowed_blocks= array( 
        'acf/homepage'
    );
}

3 Answers
3

It’s theoretically possible but would require reloading the page again because the page template can be changed after the content is initially loaded for editing; unlike a post-type.

(Maybe try to use UpdateSettings to do this although documentation is scarce.

Leave a Comment