register_block_pattern( string $pattern_name, array $pattern_properties ): bool
Registers a new block pattern.
Contents
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
stringRequired. A human-readable title for the pattern.content
stringRequired. Block HTML markup for the pattern.description
stringOptional. 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
intOptional. The intended width of the pattern to allow for a scaled preview within the pattern inserter.inserter
boolOptional. 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. forcore/template-part
you can specify areas likecore/template-part/header
orcore/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.
Return
bool True if the pattern was registered with success and false otherwise.
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 );
}
Changelog
Version | Description |
---|---|
5.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Per the Block Patterns documentation in the Block Editor Handbook, the $pattern_properties array includes:
and the example function given is:
Top ↑
Feedback
As an addition to the category, I would like to give an example for those who did not know what syntax to use. Since a block pattern can be assigned to multiple categories you can use an array to place it in the right category. example:
how to define/register a category can be found here: https://developer.wordpress.org/reference/functions/register_block_pattern_category/ — By MangoWambo —
The docs & handbook don’t seem to mention it, but I gather
register_block_pattern()
should be called from a handler attached to theinit
hook.Top ↑
Feedback
Good call! Registering the block pattern straight from functions.php results in the editor crashing with an obscure JS error after opening the patterns pannel, without any PHP errors or warnings being triggered. Calling
register_block_pattern
inside the init hook solves this perfectly! Thanks! — By Jules Colle —Just a heads up for block theme developers: since WordPress 6.0 you can also register patterns in a block theme simply by placing PHP files with patterns in your theme’s
/patterns
subfolder.A basic example of how to register a new pattern block.
If the core block patterns are removed via:
Make sure that there is at least one block pattern category registered. The block editor crashes if none are present.