block_categories_all

最后更新于:2021-11-25 21:30:41

apply_filters( ‘block_categories_all’, array[] $block_categories, GC_Block_Editor_Context $block_editor_context )

Filters the default array of categories for block types.

参数

$block_categories

(array[])
Array of categories for block types.

$block_editor_context

(GC_Block_Editor_Context)
The current block editor context.

源文件

文件: gc-includes/block-editor.php

View on Trac

/**
 * Adding a new (custom) block category.
 *
 * @param   array $block_categories                         Array of categories for block types.
 * @param   GC_Block_Editor_Context $block_editor_context 	The current block editor context.
 */
function gcdocs_add_new_block_category( $block_categories, $block_editor_context ) {
	// You can add extra validation such as seeing which post type
	// is used to only include categories in some post types.
	// if ( in_array( $block_editor_context->post->post_type, ['post', 'my-post-type'] ) ) { ... }

	return array_merge(
		$block_categories,
		[
			[
				'slug'  => 'my-block-category',
				'title' => esc_html__( 'My Block Category', 'text-domain' ),
				'icon'  => 'gechiui', // Slug of a GeChiUI Dashicon or custom SVG
			],
		]
	);
}

add_filter( 'block_categories_all', 'gcdocs_add_new_block_category' );

/**
 * Adding a new (custom) block category.
 *
 * @param   array $block_categories                                Array of categories for block types.
 * @param   GC_Block_Editor_Context|string $block_editor_context   The current block editor context, or a string defining the context.
 */
function gcdocs_add_new_block_category( $block_categories, $block_editor_context ) {
    // Check the context of this filter, return default if not in the post/page block editor.
    // Alternatively, use this check to add custom categories to only the customizer or widget screens.
    if ( ! ( $block_editor_context instanceof GC_Block_Editor_Context ) ) {
        return $block_categories;
    }

    // You can add extra validation such as seeing which post type
    // is used to only include categories in some post types.
    // if ( in_array( $block_editor_context->post->post_type, ['post', 'my-post-type'] ) ) { ... }
 
    return array_merge(
        $block_categories,
        [
            [
                'slug'  => 'my-block-category',
                'title' => esc_html__( 'My Block Category', 'text-domain' ),
                'icon'  => 'gechiui', // Slug of a GeChiUI Dashicon or custom SVG
            ],
        ]
    );
}
 
add_filter( 'block_categories_all', 'gcdocs_add_new_block_category' );

if ( version_compare( $GLOBALS['gc_version'], '5.8-alpha-1', '<' ) ) {
	add_filter( 'block_categories', 'gcdocs_add_block_category', 10, 2 );
} else {
	add_filter( 'block_categories_all', 'gcdocs_add_block_category', 10, 2 );
}

/**
 * Adding a new (custom) block category.
 *
 * @param   array                   $block_categories       Array of categories for block types.
 * @param   GC_Block_Editor_Context $block_editor_context   The current block editor context.
 */
function gcdocs_add_block_category( $block_categories, $block_editor_context ) {
	return array_merge(
		$block_categories,
		array(
			array(
				'slug'  => 'block-slug',
				'title' => __( 'Block Category Title', 'text-domain' ),
			),
		)
	);
}