The register_block_pattern_category() WordPress PHP function registers a new block pattern category.
Usage
register_block_pattern_category( 'category_name', array( 'label' => __( 'Category Label', 'text-domain' ) ) );
Parameters
- category_name (string) – Required. The pattern category name, including the namespace.
- category_properties (array) – Required. List of properties for the block pattern category. Accepted argument: ‘label’ (string) – A human-readable label for the pattern category.
More information
See WordPress Developer Resources: register_block_pattern_category()
Note: This function should be called from a handler attached to the init
hook.
Examples
Register a Custom Block Pattern Category
In this example, we will register a custom block pattern category called “Hero” in our plugin.
function my_plugin_register_hero_category() { register_block_pattern_category( 'hero', array( 'label' => __( 'Hero', 'my-plugin' ) ) ); } add_action( 'init', 'my_plugin_register_hero_category' );
Register Multiple Custom Block Pattern Categories
In this example, we will register multiple custom block pattern categories at once.
function my_plugin_register_categories() { $categories = array( 'hero' => __( 'Hero', 'my-plugin' ), 'testimonial' => __( 'Testimonial', 'my-plugin' ), 'pricing' => __( 'Pricing', 'my-plugin' ) ); foreach ( $categories as $slug => $label ) { register_block_pattern_category( $slug, array( 'label' => $label ) ); } } add_action( 'init', 'my_plugin_register_categories' );
Check if a Block Pattern Category is Registered
In this example, we will check if a block pattern category called “Hero” is already registered.
function my_plugin_is_hero_registered() { $categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); foreach ( $categories as $category ) { if ( $category['name'] === 'hero' ) { return true; } } return false; }
Register a Block Pattern Category Conditionally
In this example, we will register a block pattern category called “Hero” only if it’s not already registered.
function my_plugin_register_hero_category_conditionally() { if ( ! my_plugin_is_hero_registered() ) { register_block_pattern_category( 'hero', array( 'label' => __( 'Hero', 'my-plugin' ) ) ); } } add_action( 'init', 'my_plugin_register_hero_category_conditionally' );
Unregister a Block Pattern Category
In this example, we will unregister a block pattern category called “Hero.”
function my_plugin_unregister_hero_category() { WP_Block_Pattern_Categories_Registry::get_instance()->unregister( 'hero' ); } add_action( 'init', 'my_plugin_unregister_hero_category' );