Block Templates – Prefill New Posts with Any Blocks You Want

YouTube video

Written By

Jonathan Jernigan

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' );

Signup for the most inconsistent newsletter this side of the Mississippi

Delivered on a regular-as-I-can basis, I'll share with you the tl;dr of new blog posts and videos, exciting announcements, and other valuable information from around the WordPress ecosphere. You'll never get more than one email per week from me.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.