Discover in this tutorial how to get and display all block pattern categories of a WordPress installation using code.
In a WordPress installation there can be several patterns, some added by the WordPress core and others by themes (and/or plugins). Therefore, in some cases you may be interested in displaying (or getting) all the block pattern categories of a WordPress installation.
With this information you can decide if you prefer that some of them are unregister so that they are not displayed in the editor.
In a recent tutorial I explained how you can display all the block patterns registered in a WordPress.
And in this tutorial you are going to learn how to do the same but with all the block pattern categories.
Depending on the installation the list will be different. For example, this would be the result for a “bare” WordPress installation, one with an OsomPress theme and one with the Twenty Twenty-Two theme:
buttons columns featured gallery header text query
WordPress core
osom buttons columns featured gallery header text query
WordPress core + OsomPress theme
featured footer header query pages buttons columns gallery text
WordPress core + Twenty Twenty-Two theme
Let’s see how you can achieve it.
Steps to display all registered block pattern categories in a WordPress site using a shortcode
1. Create the shortcode
First you are going to encapsulate the list of registered block pattern categories inside a shortcode so you can display it wherever you want.
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
With this code you create a shortcode that allows you to display a list with all the pattern categories of the WordPress installation using the [pattern_category_list] shortcode.
To get the list you use the WP_Block_Pattern_Categories_Registry
class along with the get_instance
and get_all_registered
methods in combination with a loop (foreach).
In this case, only the name of the block pattern is displayed. In a basic WordPress installation the listing will look like this:
buttons columns featured gallery header text query
Instead, if the theme adds its own patterns in addition to the native WordPress ones you will see the theme’s ones. Something like this:
osom buttons columns featured gallery header text query
In this case, the ones registered by the OsomPress Uprising theme (which adds the “Osom” category) are also displayed.
But, for example, the Twenty Twenty-Two theme being a block theme adds a category called “footer”.
featured footer header query pages buttons columns gallery text
And interestingly, the Twenty Twenty-One theme adds a “twenty-twenty” category that Twenty Twenty-Two does not register
That is, depending on the theme and plugins you are using this listing will change.
To be able to see the list there is only one step left.
2. Insert the shortcode where you want
The advantage of shortcodes is that you can insert them anywhere on the web.
You only have to type [pattern_category_list]
where you want it to be displayed.
Important:
- If you are using the classic editor, use the HTML tab to enter the shortcode.
- In case you are already using the new block editor (Gutenberg) you can add it to the normal paragraph block, the HTML block or the shortcode block. All of them will render the content correctly.
Conclusions
Now you know how to get and display a list with all the categories of block patterns registered in a WordPress installation to decide if you need them all or not.
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. 😉