Block Editor PHP Intermediate

Give a Post Type a Block Template

Make every new entry of a post type start with a fixed set of blocks, and optionally stop editors adding or removing blocks.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 17 lines
add_filter( 'register_post_type_args', function ( array $args, string $post_type ): array {
	if ( 'post' !== $post_type ) {
		return $args;
	}

	$args['template'] = [
		[ 'core/paragraph', [ 'placeholder' => 'Summary (1–2 sentences)' ] ],
		[ 'core/image', [] ],
		[ 'core/heading', [ 'level' => 2, 'content' => 'Details' ] ],
		[ 'core/paragraph', [ 'placeholder' => 'Write the details…' ] ],
	];

	// 'all' = can't add, move or remove blocks; 'insert' = can't add or remove; false = no lock.
	$args['template_lock'] = false;

	return $args;
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

For structured content like team members, case studies or events, every entry should have the same parts in the same order. A block template sets that up, and template locking keeps it that way.

How it works

Post types can define a template: a list of block names and their attributes. New posts open with those blocks already in place. The register_post_type_args filter adds a template to any post type, including built-in ones like post and ones registered by plugins.

template_lock set to 'all' stops blocks being added, moved or removed; 'insert' allows moving but not adding or removing; false makes the template a starting point only.

Things to watch

  • Templates apply to new posts. Existing posts aren’t changed, but a strict lock can show a “content doesn’t match the template” warning on old posts.
  • Change 'post' to your custom post type’s name.

How to test it

Create a new post. It should open with the summary paragraph, image, Details heading and paragraph.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only