https://developer.wordpress.org/reference/classes/wp_block_type_registry/get_all_registered/Block Editor is the answer of WordPress to third-party page builders such as Elementor and WP Bakery. The Block Editor has made the process of content creation in WordPress much more intuitive and user-friendly. Block Editor uses different modules known as blocks which can be inserted in a page or post to add content to it. WordPress comes bundled with a set of core blocks such as paragraph, image, cover and many more. It is also possible to create new custom blocks and provide them as third party plugins or even integrated in the theme. In this article, we see how we can get a list of all the blocks in WordPress Block Editor.
In order to retrieve a list of all registered blocks, we utilize WordPress’ WP_Block_Type_Registry class. Before moving ahead, we need to discuss briefly about this class. This core class is responsible for interacting with the blocks. It was introduced in WordPress 5.0 along with the introduction of Block Editor in WordPress. This class has several methods which we can use to interact with the blocks. In order to get a list of all blocks in WordPress, we are going to use one of the methods of this class–get_all_registered().
Getting All the Blocks in WordPress
So, in order to get all the blocks in WordPress Block Editor, we add the following code to your WordPress Theme. The recommended approach is creating a new Child Theme and adding the code to functions.php file in that Child Theme. If you would like to know more about Child Themes, check out WordPress’ Official Codex page.For the sake of simplicity, we are going to add the code in our theme’s functions.php.
1 2 3 4 5 6 7 8 |
function my_theme_all_blocks() { $blocks = WP_Block_Type_Registry::get_instance()->get_all_registered(); $blocks = array_keys($blocks); return $blocks; } add_action('init', 'my_theme_all_blocks'); |
In this code, we are hooking the function my_theme_all_blocks to init action hook. By the time this hook is fired, all plugins have been initialized and users have been authenticated. We can also use wp_loaded when WordPress loads completely.
We get the instance of the WP_Block_Type_Registry class using get_instance() method. Now, we can access a bunch of methods to interact with the blocks or get the relevant data. You can take a look at all the available methods here. For our scenario, I’m going to use the get_all_registered() method.
We get an associative array with block name (namespace/slug) as the key and WP_Block_Type object as the value for each registered block. To get an array with names of all the blocks, we use the array_keys() PHP function.
Conclusion
So, that was it! We can get the list of all blocks in WordPress Block Editor using one of the methods in WP_Block_Type_Registry class. We can further refine the list to filter out blocks based on certain parameters such as author, name, plugin etc. In this article, I have shown how we can disable core blocks in WordPress. We are using a similar method to filter out core WordPress Blocks and disabling them.
Hope you got to learn something new from this article. We regularly post tips, tricks and tutorials related to WordPress and Web Development in general. You can read more of our amazing article on our blog. Also, you can checkout our archive of WordPress Themes for your next WordPress project.