Support

Account

Home Forums General Issues register custom GB-block category

Solved

register custom GB-block category

  • How can i register my custom Gutenberg Block category?

    I’ve tried something like:

    
    function my_plugin_block_categories( $categories, $post ) {
    	if ( $post->post_type !== 'my_post_type' ) {
    		return $categories;
    	}
    	return array_merge(
    		array(
    			array(
    				'slug' => 'example',
    				'title' => 'Example',
    				'icon'  => 'wordpress',
    			),
    		),
    		$categories
    	);
    }
    add_filter( 'block_categories', 'my_plugin_block_categories', 10, 2 );
    acf_register_block_type(array(
    	...
    	'category'          => 'example',
    	...

    However, no category with the name is displayed. Neither in posts nor pages.

  • 🙄 Of course, I found the solution shortly after the post.

    function example_block_category( $categories, $post ) {
    	return array_merge(
    		$categories,
    		array(
    			array(
    				'slug' => 'example',
    				'title' => 'Example',
    			),
    		)
    	);
    }
    add_filter( 'block_categories', 'example_block_category', 10, 2);
  • Just in case anyone gets here after searching for how to do this, after WordPress 5.8 the filter to be used is now block_categories_all.

    So, all would stay the same but use that instead:

    `
    add_filter(‘block_categories_all’, ‘example_block_category’, 10, 2);
    `

    See: https://developer.wordpress.org/reference/hooks/block_categories_all/

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.