Block Templates – Prefill New Posts with Any Blocks You Want

Gutenberg

August 30, 2023

YouTube video

I wanted to show you something really cool I recently discovered called Block Templates. With Block Templates, you can predefine a set of blocks that automatically load when creating a new post. Whether you want to maintain brand consistency, ensure certain key elements are always present, or simply streamline your content creation process, Block Templates can be a really powerful feature for you to take advantage of.

Basic code snippet:

function myplugin_register_template() {
    $post_type_object = get_post_type_object( 'post' );
    $post_type_object->template = array(
        array( 'core/heading', array( 'level' => 2 ) ), // This is for the headline.
        array( 'core/paragraph' ),  // This is for the body text.
    );
    $post_type_object->template_lock = 'all';  // This locks the block so they can't be moved or deleted.
}
add_action( 'init', 'myplugin_register_template' );

Join the Inside Link Community

Get access to all courses, weekly office hours, live build sessions, and an active community of WordPress professionals.

Learn More →