Learn in this tutorial how to create custom categories for WordPress block patterns using code.
WordPress 5.5 introduced many new features, including the option to use block patterns in the editor.
For one thing, it included several native block patterns:
- Two buttons
- Three buttons
- Two columns of text
- Two columns of text with images
- Three columns of text with buttons
- Two contiguous images
- Large headrest
- Large header with a header and a button
- Large headline with a paragraph
- Header and paragraph
That you can create your own patterns in a very simple way, as I explained in this tutorial.
In it you saw that you could
register a new pattern using the
register_block_pattern function and give it an already existing category:
- Buttons (‘buttons’)
- Columns (‘columns’)
- Gallery (‘gallery’)
- Headers (‘header’)
- Text (‘text’)
Or you could also create new custom categories.
Are you interested? Here’s how to do it:
Snippet to create custom categories for block patterns in WordPress
If you remember, once you have created the block pattern, there are two options, register it through a plugin or directly in a child theme.
- If you’ve done it directly in the child theme, add the following snippet to the functions.php.
- If you’ve done it in a plugin, add it to the end of the plugin file.
In this case, the new category is “Osom Patterns” but you can register any name you want.
You can replace ‘text-domain’ with your child theme (e.g. ‘genesis-sample’) or your plugin.
The only thing left to do is to use the new category when registering the block pattern:
'categories' => array( 'Osom Patterns' )
It will be displayed like this in the pattern section:
You can also use more than one category. For example, a native one, like headers, and a custom one like the one you just created. To do this, replace the previous one with:
'categories' => array( 'Osom Patterns', 'header' ),
This will be shown in both:
The arrival of block patterns in the WordPress editor opens up many possibilities. If you want to have your own patterns sorted into custom categories, now you know how to do it.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉