Blog · Gutenberg

Register Block Templates From a WordPress Plugin

Oct 28, 2024By Vladimir Mrkela
Share LinkedInX
Register Block Templates From a WordPress Plugin

The upcoming WordPress 6.7 introduces an API for plugins to register block templates. As of October 28, 2024, test it on a prerelease staging site, not production. See the WordPress developer note.

Registering block templates from a plugin matters because it makes those layouts available to the right content types while giving site owners a consistent starting point.

The article explains how block templates fit into a plugin and what to consider when registering one.

How Do WordPress Block Templates Work?

A site block template defines the layout used to render content. A plugin-registered template can appear in the Site Editor or be made available as a per-post custom template. This differs from a post-type starter block template or an inserted block pattern.

WordPress block templates give pages and posts a ready-made layout you can adjust in the editor.

A site template uses block markup to arrange elements such as a header, post content, and footer. It controls the page layout; it does not automatically insert starter blocks into the post content.

The template hierarchy affects which layout renders. Plugin templates can supply hierarchy templates, but theme templates take priority over plugin-registered templates.

You can build layouts yourself or adapt designs from block pattern libraries. Keep the markup valid, and test the template with your active theme.

Check that its styles and block settings work as expected, since themes can change how the same layout looks.

Choose the Post Type for Your Plugin Template

Before registering a block template, decide which post type should offer it in the editor. Your choice determines where users can start with your layout, so match the template to the content they’ll create. For example, a recipe layout may fit a custom recipe post type, while a landing page design belongs with pages.

Check supported content types

List the post types your plugin supports, then choose the one that best fits the template’s purpose. WordPress includes posts and pages, and plugins can add custom post types. Don’t assume every type should use the same layout.

Choose where to offer the template

Use post_types to offer the template for suitable post types. This controls availability by post type, not by individual items or their fields. If the layout relies on specific content or fields, explain those requirements to editors.

Register Your Plugin’s Block Template

Once you’ve chosen a post type, register your template so users can select it as a template for matching content.

Add the registration code to your Plugin bootstrap, where WordPress loads your plugin. Use the register_block_template function on the init hook, and pass the post type and template details in its arguments.

The following complete plugin registers a “Project Brief” template for pages. On a staging site running a WordPress 6.7 prerelease with a block theme, save it as project-brief-example.php in its own folder under wp-content/plugins, then activate it.

PHP
<?php
/**
 * Plugin Name: Project Brief Template Example
 * Description: Registers a custom block template for pages.
 * Version: 1.0.0
 * Requires at least: 6.7
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register the Project Brief page template.
 */
function example_register_project_brief_template() {
	$result = register_block_template(
		'example-plugin//project-brief',
		array(
			'title'       => 'Project Brief',
			'description' => 'A simple layout for project pages.',
			'post_types'  => array( 'page' ),
			'content'     => '<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-group">
<!-- wp:post-title {"level":1} /-->
<!-- wp:post-content /-->
</main>
<!-- /wp:group -->',
		)
	);

	if ( is_wp_error( $result ) ) {
		error_log( 'Project Brief template registration failed: ' . $result->get_error_message() );
	}
}
add_action( 'init', 'example_register_project_brief_template' );

Create a test page, select Project Brief in its template settings, add page content, and preview it. The Post Title and Post Content blocks render that page’s title and content. This minimal layout has no header or footer; add suitable existing theme template parts if needed.

Check that your WordPress version supports the function before relying on it, and avoid registering the same template more than once.

Test editor compatibility by creating a new item of that post type in the editor. Confirm that your template appears in the available choices and that selecting it applies the expected layout.

If it doesn’t appear, review the hook, post type slug, and registration arguments.

Build the Template With Block Markup

Define the template’s layout as block markup. Include a Post Content block when the layout should display the content of the current post or page. Pass a single string containing the block markup as the content argument; this is different from a post-type starter template array. Use Block HTML syntax, which pairs HTML elements with WordPress block comments. For example, a paragraph can look like <!-- wp:paragraph --><p>Add your introduction here.</p><!-- /wp:paragraph -->.

Add blocks in the order you want them to appear. For blocks that contain other blocks, nest the child block markup between the parent block’s opening and closing comments. Check that every block has matching comments and valid HTML.

Text placed directly in a template is shared by items using that template. Editors enter individual page or post content in the content editor; the Post Content block displays that content. Test the template in the editor and on the front end.

Set the Template Name, Title, and Post Type

Give your template a unique name, a clear title, and the post type it should apply to.

These values tell WordPress how to identify the template and where to offer it. Set them in the template registration code before you test it in the editor.

Template Name

Pass the template name as the first argument, followed by an arguments array containing title and post_types. For Template Naming, choose a clear, unique identifier that describes the layout’s purpose, such as acme-plugin//product-feature.

You’ll use this name to identify the template when you register it, so keep it stable and avoid vague labels like custom-template.

Follow consistent Name Conventions across your plugin. A lowercase namespace helps distinguish your templates from those added by WordPress or other plugins. Separate words with hyphens, and include your plugin or project prefix to reduce naming conflicts.

For example, acme-plugin//landing-page is easier to recognize than template1.

Before registering the template, check that its name matches your plugin’s existing naming pattern. If you rename it later, update every reference to the old name, too.

Consistency makes your configuration easier to maintain and helps you find the right template in code.

Title and Post Type

Once you’ve chosen a stable template name, set its title and post_types values to control how it appears and where WordPress offers it.

Set the template title

The Template title is the label people see when choosing a template in the editor. Make it clear and specific, such as “Product Landing Page” or “Staff Profile.”

WordPress uses your template name as an identifier, so don’t rely on it to explain the template’s purpose.

Choose a post type

The post_types value lists the content types that can use the template. For example, assign a landing page template to page, or a product layout to a registered product Post type. Use an array, even when you’re targeting only one type:

PHP
'post_types' => array( 'page' ),

Check that each type exists and matches your intended editing screen. If you omit post_types, WordPress may not offer the template where you expect.

Review both values in the editor after registering your template.

Add Block Attributes and Template Parts

Define block attributes to control the content and settings each block stores.

Use existing template parts for reusable sections and configure block attributes in the markup. The 6.7 plugin template API does not register template parts.

Define Block Attributes

Blocks in the template markup can include attributes that control their settings. This is separate from registering a block pattern.

Identify the settings each block should use, such as alignment or an image, and check the block’s registered attributes. You do not need to modify an existing block’s block.json to use it in a template.

Match each type to the data you expect, or the editor may handle the value incorrectly.

Set default values when a block needs useful content before you edit it. For example, a heading attribute might use a short prompt, while an alignment attribute could default to wide.

In the template markup, provide supported attribute values in block comments. Check the result in the editor, and confirm each value appears as intended.

Keep attributes focused on editable settings, not template-part registration.

Use Template Parts

Template parts let a block theme reuse areas such as headers and footers. The 6.7 plugin template API cannot register template parts; refer to existing theme parts instead.

Patterns are registered separately with register_block_pattern(); they are not template parts.

If the active block theme provides a header or footer template part, reference its slug in a template-part block.

Keep the slug consistent with the template part reference in your block templates, or WordPress won’t find it.

Test the referenced parts in the Site Editor with the active theme.

Configure Block Defaults

You can set block attributes in the template markup and reference existing theme template parts where needed.

Set values in the block comments within the content string. Check each block’s documentation for supported attributes.

Add attributes and template parts

  1. Add the block you want to the template.
  2. Set its attributes, such as alignment, style, or placeholder text.
  3. If needed, include a template part block that references an existing theme template part.

These block settings provide defaults for the shared layout. Editors with access to template editing can adjust them; individual page content is edited separately. Confirm the template loads and test its blocks and settings.

If a value doesn’t appear, verify its name and format match the block’s expected attributes.

Test Your Plugin Template in the Editor

On a staging site, open the Site Editor and check the registered template. If you specified post_types, create a test item and check its available template choices. Review the layout and its blocks.

Check the editor experience

In the template editor, try selecting, moving, and editing the layout blocks. Changes to the shared template can affect multiple items that use it, so test with care.

Check that settings and controls appear as expected, that template blocks render correctly, and that the Post Content block displays the current item.

Next, view the page on the front end. Compare its layout with the editor preview, especially if your theme uses different styles.

Check the template at a narrow screen width, too, and confirm that text and images remain readable.

When validating block compatibility, test any blocks your template depends on, including blocks from other plugins.

Save your test content, reopen it, and confirm your changes remain intact. Remove the test page when you finish.

Fix Common Registration Problems

If your template doesn’t appear in the expected template choices, check its registration details first. Confirm that your plugin loads the registration code and that the template uses a unique slug and valid block names. A typo in either can keep WordPress from recognizing it.

Check common causes

  1. Verify Plugin dependency checks. Make sure your code runs after the required WordPress functions are available. If you register the template too early, hook the code to init.
  2. Review registration error diagnostics. Check your PHP error log and enable debugging in a test environment. Look for misspelled function names, missing commas, or invalid template data.
  3. Confirm the template type. Match the registration method to the editor you’re targeting, such as a page template or a block pattern.

After each change, reload the editor and check the template choices again. Don’t troubleshoot several changes at once, so you can identify the cause.

Maintain and Update Your Plugin Template

Keep your plugin template working by reviewing it when WordPress or your plugin changes. Test the registered template on a staging site after updates, then check its block structure, default content, and editor behavior.

If you use theme or plugin functions in the template, confirm they still exist and return the expected values.

Keep updates manageable

  1. Track changes: Use Template Versioning in your plugin’s changelog or version control. Note what changed and which WordPress versions you’ve tested.
  2. Test before release: Create a new post or page, select the template, and verify that blocks load and save correctly. Also test existing content, which may not update automatically.
  3. Plan recovery: Keep a copy of the last working template. If an update causes trouble, use Update Rollbacks to restore that version while you investigate.

Tell users what changed and whether they need to reselect the template. Test the fix before releasing it broadly.

Frequently Asked Questions

Can Users Override a Plugin-Provided Block Template?

Yes. With a compatible block theme, users can customize a plugin-provided template in the Site Editor; WordPress saves the customized template in the database. Test the result after plugin or theme updates, since a saved customization can take precedence over the supplied template.

How Do Plugin Templates Differ From Theme Templates?

Plugin templates belong to a plugin, while theme templates belong to a theme. A plugin template can be available across compatible themes, but its blocks and any referenced theme parts still need testing. Theme templates take priority over matching plugin-registered templates.

Can a Plugin Provide Multiple Templates for One Post Type?

Yes. Register each template with a unique name and include the post type in its post_types array. Editors can then choose an appropriate available template for their content.

Do Block Templates Work With Classic Themes?

Do not assume the same experience with every classic theme. Block-template support and the available editing interface depend on the theme and WordPress setup. Test the specific theme and template; for this example, use a block theme.

How Can Users Restore a Customized Template to Its Original Layout?

You can restore a customized template by opening the Site Editor, selecting Templates, and choosing Reset Template from its options. This Restore Layout action returns the template to its original design, so you’ll lose your customizations.

Tags
  • block templates
  • custom layouts
  • WordPress plugins

About the author

Vladimir Mrkela

Vladimir Mrkela

Senior WordPress Developer

I’m a senior WordPress developer and co-founder of Pixel Industry. I’ve spent more than 12 years building websites, custom plugins, and integrations for all kinds of projects. I enjoy working on complex architecture, solving tricky problems, and sharing what I learn along the way.

Ready to build?

Have a WordPress project that needs expert judgment?

Get a Free ConsultationTell us about your project and we will get back to you soon.