Using WordPress ‘register_block_pattern()’ PHP function

The register_block_pattern() WordPress PHP function registers a new block pattern in the WordPress editor.

Usage

register_block_pattern( 'your-namespace/your-pattern-name', array(
    'title'       => 'Your Pattern Title',
    'content'     => 'Your Pattern HTML Markup',
    'description' => 'Your Pattern Description',
    // Other optional properties
));

Parameters

  • $pattern_name (string) – Required. The block pattern name, including the namespace.
  • $pattern_properties (array) – Required. List of properties for the block pattern, including:
    • title (string) – Required. A human-readable title for the pattern.
    • content (string) – Required. Block HTML markup for the pattern.
    • description (string) – Optional. Visually hidden text used to describe the pattern in the inserter.
    • viewportWidth (int) – Optional. The intended width of the pattern to allow for a scaled preview within the pattern inserter.
    • inserter (bool) – Optional. Determines whether the pattern is visible in the inserter. Default is true.
    • categories (array) – Optional. A list of registered pattern categories used to group block patterns.
    • keywords (array) – Optional. A list of aliases or keywords that help users discover the pattern while searching.
    • blockTypes (array) – Optional. A list of block names including namespace that could use the block pattern in certain contexts (placeholder, transforms).
    • postTypes (array) – Optional. An array of post types that the pattern is restricted to be used with.
    • templateTypes (array) – Optional. An array of template types where the pattern fits.

More information

See WordPress Developer Resources: register_block_pattern()

Examples

Register a simple block pattern

Registers a simple block pattern with a title and content.

function my_block_pattern() {
    register_block_pattern( 'my-namespace/simple-pattern', array(
        'title'   => 'Simple Pattern',
        'content' => '<!-- wp:paragraph --><p>Hello, World!</p><!-- /wp:paragraph -->',
    ));
}
add_action( 'init', 'my_block_pattern' );

Register a block pattern with a description

Registers a block pattern with a title, content, and a description.

function my_block_pattern_with_description() {
    register_block_pattern( 'my-namespace/descriptive-pattern', array(
        'title'       => 'Descriptive Pattern',
        'content'     => '<!-- wp:paragraph --><p>Hello, World!</p><!-- /wp:paragraph -->',
        'description' => 'A simple pattern with a description.',
    ));
}
add_action( 'init', 'my_block_pattern_with_description' );

Register a block pattern with a category

Registers a block pattern with a title, content, and a specific category.

function my_block_pattern_with_category() {
    register_block_pattern( 'my-namespace/category-pattern', array(
        'title'      => 'Category Pattern',
        'content'    => '<!-- wp:paragraph --><p>Hello, World!</p><!-- /wp:paragraph -->',
        'categories' => array( 'text' ),
    ));
}
add_action( 'init', 'my_block_pattern_with_category' );

Register a block pattern with keywords

Registers a block pattern with a title,