Skip to content

Instantly share code, notes, and snippets.

@bahiirwa
Forked from zgordon/block-template.php
Created March 9, 2018 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahiirwa/9ebc13577bda1f505c88bfb500a19fc3 to your computer and use it in GitHub Desktop.
Save bahiirwa/9ebc13577bda1f505c88bfb500a19fc3 to your computer and use it in GitHub Desktop.
Example of how to add block templates to post types in WordPress
<?php
function mytheme_block_templates( $args, $post_type ) {
// Only add template to 'post' post type
// Change for your post type: eg 'page', 'event', 'product'
if ( 'post' == $post_type ) {
// Optionally lock templates from further changes
// Change to 'insert' to allow adding other blocks, but lock defined blocks
$args['template_lock'] = 'all';
// Set the template
$args['template'] = [
[
// Example of including a core image block
// Optional alignment setting
'core/image', [
'align' => 'left',
]
],
[
// Example of including a core paragraph block
// Optional alignment placeholder setting
'core/paragraph', [
'placeholder' => 'The only thing you can add',
'align' => 'right',
]
],
[
// Example of including a custom block
// Optional placeholder setting
'custom/your-blocks', [
'placeholder' => 'Custom placeholder',
]
]
];
}
return $args;
}
// Hook function into post type arguments filter
add_filter( 'register_post_type_args', 'mytheme_block_templates', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment