register_block_pattern( string $pattern_name, array $pattern_properties ): bool

Registers a new block pattern.


Parameters

$pattern_name string Required
Block pattern name including namespace.
$pattern_properties array Required
List of properties for the block pattern.
See WP_Block_Patterns_Registry::register() for accepted arguments.
More Arguments from WP_Block_Patterns_Registry::register( ... $pattern_properties ) List of properties for the block pattern.
  • 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. A description is optional, but is strongly encouraged when the title does not fully describe what the pattern does. The description will help users discover the pattern while searching.
  • 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 inserter.
    To hide a pattern so that it can only be inserted programmatically, set this to false. Default true.
  • categories string[]
    Optional. A list of registered pattern categories used to group block patterns. Block patterns can be shown on multiple categories.
    A category must be registered separately in order to be used here.
  • keywords string[]
    Optional. A list of aliases or keywords that help users discover the pattern while searching.
  • blockTypes string[]
    Optional. A list of block names including namespace that could use the block pattern in certain contexts (placeholder, transforms).
    The block pattern is available in the block editor inserter regardless of this list of block names.
    Certain blocks support further specificity besides the block name (e.g. for core/template-part you can specify areas like core/template-part/header or core/template-part/footer).
  • postTypes string[]
    Optional. An array of post types that the pattern is restricted to be used with. The pattern will only be available when editing one of the post types passed on the array. For all the other post types not part of the array the pattern is not available at all.
  • templateTypes string[]
    Optional. An array of template types where the pattern fits.

Top ↑

Return

bool True if the pattern was registered with success and false otherwise.


Top ↑

Source

File: wp-includes/class-wp-block-patterns-registry.php. View all references

function register_block_pattern( $pattern_name, $pattern_properties ) {
	return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
}


Top ↑

Changelog

Changelog
Version Description
5.5.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Christina Blust

    Per the Block Patterns documentation in the Block Editor Handbook, the $pattern_properties array includes:

    • title (required): A human-readable title for the pattern.
    • content (required): Raw HTML content for the pattern.
    • description: A visually hidden text used to describe the pattern in the inserter. A description is optional but it is strongly encouraged when the title does not fully describe what the pattern does.
    • categories: A list of pattern categories used to group block patterns. Block patterns can be shown on multiple categories.
    • keywords: Aliases or keywords that help users discover it while searching.
    • viewportWidth: Specify the width of the pattern in the inserter.

    and the example function given is:

    register_block_pattern(
        'wpdocs-my-plugin/my-awesome-pattern',
        array(
            'title'       => __( 'Two buttons', 'wpdocs-my-plugin' ),
            'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
            'content'     => "<!-- wp:buttons {\"align\":\"center\"} -->\n<div class=\"wp-block-buttons aligncenter\"><!-- wp:button {\"backgroundColor\":\"very-dark-gray\",\"borderRadius\":0} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius\">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button -->\n\n<!-- wp:button {\"textColor\":\"very-dark-gray\",\"borderRadius\":0,\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius\">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
        )
    );
  2. Skip to note 2 content
    Contributed by mikehealy

    The docs & handbook don’t seem to mention it, but I gather register_block_pattern() should be called from a handler attached to the init hook.

    function wpdocs_register_my_patterns() {
      register_block_pattern( ... );
    }
    
    add_action( 'init', 'wpdocs_register_my_patterns' );
  3. Skip to note 4 content
    Contributed by thejaydip

    A basic example of how to register a new pattern block.

    function wpdocs_register_block_patterns() {
            register_block_pattern(
                'wpdocs/my-example',
                array(
                    'title'         => __( 'My First Block Pattern', 'textdomain' ),
                    'description'   => _x( 'This is my first block pattern', 'Block pattern description', 'textdomain' ),
                    'content'       => '<!-- wp:paragraph --><p>A single paragraph block style</p><!-- /wp:paragraph -->',
                    'categories'    => array( 'text' ),
                    'keywords'      => array( 'cta', 'demo', 'example' ),
                    'viewportWidth' => 800,
                )
            );
    }
    add_action( 'init', 'wpdocs_register_block_patterns' );

You must log in before being able to contribute a note or feedback.