Reputation: 508
For one of my project I'm trying to use pre-defined Gutenberg blocks on custom page template. I' using Block Lab WP Plugin to create custom Gutenberg blocks. But when I used on page template, that doesn't work.
Is it possible to use Gutenberg blocks on page template? So that can be instantly available to use when anyone change the page template?
Thanks in advance
Upvotes: 2
Views: 1531
Reputation: 106
Real page templates are not supported yet with Gutenberg.
But I made a small plugin for this to study this idea too. You can download it and test it. BUT, it is meant for developers that understand Gutenberg (a bit) and can modify/extend this plugin. And for sure not ready for Production.
It will create a CPT "Page template", in where you define your page template blocks. When you are in the "pages", create a new page, and select the page template from the (new) upper-right corner sidebar.
Upvotes: 1
Reputation: 527
You need to use Custom Post Type for blocks. What it does is you can register your own post template with predefined blocks in it. Which I believe is what you are looking for. Here's example of that -
function myplugin_register_book_post_type() {
$args = array(
'public' => true,
'label' => 'Books',
'show_in_rest' => true,
'template' => array(
array( 'core/image', array(
'align' => 'left',
) ),
array( 'core/heading', array(
'placeholder' => 'Add Author...',
) ),
array( 'core/paragraph', array(
'placeholder' => 'Add Description...',
) ),
),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'myplugin_register_book_post_type' );
This function registers a CPT and via template option, specifying which block are inside of it. You can lock(no outside block allowed), loosely lock(certain blocks allowed) or allow other blocks to be inserted as well.
Upvotes: 1