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

Question

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'
    );
}
0
, , UntitledGraphic 3 years 2020-07-09T09:11:50-05:00 0 Answers 68 views 0

Leave an answer

Browse
Browse