Register and Use WordPress Block Patterns for Reusable Layouts

Register and use WordPress block patterns for reusable layouts

Block patterns are ready-made arrangements of blocks that an editor inserts with one click: a hero section, a testimonial grid, a pricing table. For the block editor they are what shortcodes and copy-pasted HTML were for the classic one, only better: after insertion they are normal blocks that can be edited. A theme can bundle its own patterns, and since WordPress 6.0 it is enough to put a PHP file into the theme’s patterns folder.

Problem: The designers of an agency site rebuild the same eight layouts (heroes, testimonials, CTA banners, pricing tables) from scratch on every new page, spending half an hour per page on layout instead of content.

Solution: Build each layout once in the editor, save its markup as a pattern file in patterns/, group the patterns in a custom category, and let WordPress register them from the file headers.

A pattern file

Build the layout in the editor, open the Code editor view (or “Copy” in the block toolbar), and paste the markup into patterns/hero-split.php:

<?php
/**
 * Title: Hero, split image
 * Slug: agency/hero-split
 * Categories: agency, featured
 * Keywords: hero, banner, split, header
 * Description: A section with text on the left and an image on the right.
 */
?>
<!-- wp:columns {"verticalAlignment":"center","align":"wide"} -->
<div class="wp-block-columns alignwide are-vertically-aligned-center">
    <!-- wp:column {"width":"55%"} -->
    <div class="wp-block-column" style="flex-basis:55%">
        <!-- wp:heading {"fontSize":"x-large"} -->
        <h2 class="wp-block-heading has-x-large-font-size"><?php esc_html_e( 'A headline that converts', 'agency-theme' ); ?></h2>
        <!-- /wp:heading -->

        <!-- wp:paragraph -->
        <p><?php esc_html_e( 'Two or three sentences that explain the value.', 'agency-theme' ); ?></p>
        <!-- /wp:paragraph -->

        <!-- wp:buttons -->
        <div class="wp-block-buttons">
            <!-- wp:button -->
            <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php esc_html_e( 'Get started', 'agency-theme' ); ?></a></div>
            <!-- /wp:button -->
        </div>
        <!-- /wp:buttons -->
    </div>
    <!-- /wp:column -->

    <!-- wp:column {"width":"45%"} -->
    <div class="wp-block-column" style="flex-basis:45%">
        <!-- wp:image {"sizeSlug":"large"} -->
        <figure class="wp-block-image size-large"><img src="<?php echo esc_url( get_theme_file_uri( 'assets/images/hero.jpg' ) ); ?>" alt=""/></figure>
        <!-- /wp:image -->
    </div>
    <!-- /wp:column -->
</div>
<!-- /wp:columns -->

The header fields: Title and Slug (with your namespace) are required; Categories, Keywords, Description, Block Types, Viewport Width, Inserter are optional. Because the file is PHP, texts can be translatable and the image can come from the theme folder.

Use h2 in patterns meant for the content: the page title is already the h1.

A custom category

Theme files register the patterns, but a new category has to be registered in code:

<?php
add_action( 'init', function () {
    register_block_pattern_category( 'agency', array(
        'label'       => __( 'Agency', 'agency-theme' ),
        'description' => __( 'Layouts for agency marketing pages.', 'agency-theme' ),
    ) );
} );

Registering in code

Plugins (and themes that keep patterns elsewhere) use register_block_pattern():

<?php
add_action( 'init', function () {
    register_block_pattern( 'agency/cta-banner', array(
        'title'      => __( 'CTA banner', 'agency-theme' ),
        'categories' => array( 'agency' ),
        'keywords'   => array( 'cta', 'banner', 'call to action' ),
        'content'    => '<!-- wp:group {"layout":{"type":"constrained"}} --><div class="wp-block-group"><!-- wp:heading --><h2 class="wp-block-heading">' . esc_html__( 'Ready to start?', 'agency-theme' ) . '</h2><!-- /wp:heading --></div><!-- /wp:group -->',
    ) );
} );

Do not register the same pattern both ways: a slug from a file in patterns/ and the same slug in register_block_pattern() collide.

Synced and unsynced

An inserted pattern is a copy: changing it on one page does not affect the others. Since WordPress 6.3 editors can also create synced patterns (formerly “reusable blocks”): stored as wp_block posts, edited once and updated everywhere. Use synced ones for content that must be identical (a contact box), regular patterns for layouts that are only a starting point.

NOTE: The Patterns tab also shows patterns from the wordpress.org Pattern Directory. To show only your own (for brand consistency or privacy), switch off the remote ones with add_filter( 'should_load_remote_block_patterns', '__return_false' ); and, if needed, the core ones with remove_theme_support( 'core-block-patterns' );. Patterns that need their own behavior are a job for a custom block, see building a dynamic block.