Add Custom Block Patterns and Template Parts to a WordPress Theme

Block patterns and template parts in a block theme

A block theme ships two kinds of reusable layout. Template parts (parts/header.html, parts/footer.html) are shared areas that every template includes and that editors change in one place. Block patterns (patterns/*.php) are ready-made sections, such as a hero or a pricing table, that editors insert into a page and then edit freely. Since WordPress 6.0 both are picked up from their folders without any registration code.

Problem: Editors build the same hero section by hand on every landing page, and the header and footer of the block theme are copied into each template.

Solution: Put the header and footer into parts/ and include them with the Template Part block, declare their areas in theme.json, and add sections as PHP files in patterns/ with a file header (Title, Slug, Categories), using PHP only for translatable strings and theme URLs.

Template parts

<!-- templates/page.html -->
<!-- wp:template-part {"slug":"header","area":"header","tagName":"header"} /-->

<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-group">
    <!-- wp:post-title /-->
    <!-- wp:post-content {"layout":{"type":"constrained"}} /-->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer","area":"footer","tagName":"footer"} /-->
{
    "version": 2,
    "templateParts": [
        { "name": "header", "title": "Header", "area": "header" },
        { "name": "footer", "title": "Footer", "area": "footer" }
    ]
}

The templateParts entries in theme.json give the parts a title and an area, so the Site Editor offers them in the right place. When an editor changes the header in the Site Editor, the change is saved in the database and overrides the file; “Clear customizations” goes back to the theme version.

A pattern

<?php
/**
 * Title: Hero with call to action
 * Slug: hello-theme/hero-cta
 * Categories: featured, banner
 * Keywords: hero, cta
 * Viewport Width: 1280
 */
?>
<!-- wp:group {"align":"full","backgroundColor":"primary","textColor":"base","layout":{"type":"constrained"},"style":{"spacing":{"padding":{"top":"var:preset|spacing|60","bottom":"var:preset|spacing|60"}}}} -->
<div class="wp-block-group alignfull has-base-color has-primary-background-color has-text-color has-background" style="padding-top:var(--wp--preset--spacing--60);padding-bottom:var(--wp--preset--spacing--60)">
    <!-- wp:heading {"textAlign":"center","level":1} -->
    <h1 class="wp-block-heading has-text-align-center"><?php echo esc_html__( 'Launch your shop this week', 'hello-theme' ); ?></h1>
    <!-- /wp:heading -->

    <!-- wp:buttons {"layout":{"type":"flex","justifyContent":"center"}} -->
    <div class="wp-block-buttons">
        <!-- wp:button -->
        <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php echo esc_html__( 'Get started', 'hello-theme' ); ?></a></div>
        <!-- /wp:button -->
    </div>
    <!-- /wp:buttons -->
</div>
<!-- /wp:group -->

The file name does not matter; the Slug header must be unique and start with the theme slug. The colour and spacing names refer to presets in theme.json, so the pattern follows the theme’s palette. The easiest way to get valid block markup is to build the section in the editor, copy it with “Copy all blocks” and paste it into the file.

Other useful headers: Block Types: core/template-part/header offers the pattern when a header is created, Post Types: page limits it to pages, Inserter: no hides it from the inserter (for patterns used only in templates via <!-- wp:pattern {"slug":"hello-theme/hero-cta"} /-->).

Custom categories

add_action( 'init', function () {
    register_block_pattern_category( 'hello-theme', [ 'label' => __( 'Hello Theme', 'hello-theme' ) ] );
} );

NOTE: A pattern is copied into the page when it is inserted; later changes to the file do not update pages that already use it. For a section that must stay the same everywhere, use a synced pattern (called “reusable block” before WordPress 6.3) or a template part. Classic themes can register patterns too, with register_block_pattern() on init.