How to Display WordPress Block Patterns Programmatically (No Plugins Required)

WordPress Block Patterns Programmatically

I was customizing a WordPress installation and I wanted to output a WordPress Block Pattern programmatically. The pattern had been created in the admin, using the post editor. I tried getting an instance of the pattern using it’s slug as such:

$pattern = WP_Block_Patterns_Registry::get_instance()->get_registered( 'my-pattern-slug );
if ( $pattern && isset( $pattern['content'] ) ) {
    echo  do_blocks( $pattern['content'] );
}
var_dump( $pattern );

All this does is return null.

WordPress Patterns – Stored in Posts Table

The patterns are stored as JSON objects in the posts table. I wondered if I should just pull the JSON then process it into the proper HTML? Is there a better way?

I found I was on the right track, but the issue arises because WP_Block_Patterns_Registry::get_instance()->get_registered( 'my-pattern-slug' ); only works for registered block patterns (those added via register_block_pattern()). However, patterns stored in the Posts table (created via the editor) are not “registered” in the global registry.

Ok, so that’s why, but the same questions existed for proceeding and achieving my intended result.

How to Retrieve & Output WordPress Patterns (Stored in Posts Table)

You can fetch the pattern from the database and process its JSON manually. Here’s the best way:

1. Query the Pattern from the wp_posts Table

Patterns stored in the editor are saved as wp_posts with post_type = wp_block.

$pattern_post = get_page_by_path( 'my-pattern-slug', OBJECT, 'wp_block' );

if ( $pattern_post ) {
   // Process the content as blocks
   echo do_blocks( $pattern_post->post_content );
} else {
   echo 'Pattern not found.';
}

get_page_by_path() retrieves the pattern using its slug.

do_blocks() processes the raw block content into HTML.

*Note: You can get the pattern slug by viewing the JSON output from the pattern editor page:

Display WordPress Block Patterns Programmatically - getting the slug

Alternative: Direct Query

If slugs aren’t working as expected, you can use WP_Query:

$pattern_query = new WP_Query([
'post_type' => 'wp_block',
'name' => 'my-pattern-slug',
'posts_per_page' => 1
]);

if ( $pattern_query->have_posts() ) {
$pattern_post = $pattern_query->posts[0];
echo do_blocks( $pattern_post->post_content );
} else {
echo 'Pattern not found.';
}

When to Use WP_Block_Patterns_Registry


If the pattern was registered using register_block_pattern(), then you can use the get_registered function instead:

$pattern = WP_Block_Patterns_Registry::get_instance()->get_registered( 'my-pattern-slug' );
if ( $pattern && isset( $pattern['content'] ) ) {
echo do_blocks( $pattern['content'] );
}

But this won’t work for patterns created in the editor.

So, in order to How to Display WordPress Block Patterns programmatically without plugins, you have the following options:

If the pattern was added via the editor, use get_page_by_path() or a WP_Query to fetch and render it.

If the pattern was registered via PHP, WP_Block_Patterns_Registry is the correct approach.

Output Option: Adding your pattern directly to the_content()

Method 1: Append the Pattern to the_content()

Add this to functions.php (or a custom plugin):

function append_pattern_to_content( $content ) {
if ( is_single() || is_page() ) { // Ensure it only applies to posts or pages
$pattern_post = get_page_by_path( 'my-pattern-slug', OBJECT, 'wp_block' );

if ( $pattern_post ) {
$pattern_html = do_blocks( $pattern_post->post_content );
$content .= $pattern_html; // Append to the content
}
}

return $content;
}
add_filter( 'the_content', 'append_pattern_to_content' );

Method 2: Prepend the Pattern to the_content()

If you want the pattern before the content instead:

function prepend_pattern_to_content( $content ) {
if ( is_single() || is_page() ) {
$pattern_post = get_page_by_path( 'my-pattern-slug', OBJECT, 'wp_block' );

if ( $pattern_post ) {
$pattern_html = do_blocks( $pattern_post->post_content );
$content = $pattern_html . $content; // Prepend pattern
}
}

return $content;
}
add_filter( 'the_content', 'prepend_pattern_to_content' );

Method 3: Insert Pattern in the Middle of Content

If you want to insert the pattern after the first paragraph, use this approach:

function insert_pattern_after_paragraph( $content ) {
if ( is_single() || is_page() ) {
$pattern_post = get_page_by_path( 'my-pattern-slug', OBJECT, 'wp_block' );

if ( $pattern_post ) {
$pattern_html = do_blocks( $pattern_post->post_content );

// Split content into paragraphs
$paragraphs = explode( '</p>', $content );

// Insert pattern after first paragraph
if ( count( $paragraphs ) > 1 ) {
$paragraphs[0] .= '</p>'; // Close first paragraph
array_splice( $paragraphs, 1, 0, $pattern_html ); // Insert pattern
$content = implode( '', $paragraphs ); // Reconstruct content
}
}
}

return $content;
}
add_filter( 'the_content', 'insert_pattern_after_paragraph' );

Final Notes

  1. Ensure your theme supports block styles by adding:
    add_theme_support( 'wp-block-styles' );
  2. Check if get_page_by_path() works correctly by debugging:
    error_log( print_r( $pattern_post, true ) );
  3. For dynamic patterns, consider using render_block() instead of do_blocks():
    $blocks = parse_blocks( $pattern_post->post_content ); foreach ( $blocks as $block ) { $content .= render_block( $block ); }

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.