block_categories

最后更新于:2021-11-25 21:28:58

apply_filters_deprecated( ‘block_categories’, array[] $block_categories, GC_Post $post )

Filters the default array of categories for block types.

参数

$block_categories

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

$post

(GC_Post)
Post being loaded.

源文件

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

View on Trac

/**
 * Creating a new (custm) block category.
 *
 * @param   array $categories     List of block categories.
 * @return  array
 */
function gcdocs_new_block_category( $categories ) {
    // Plugin’s block category title and slug.
    $block_category = array( 'title' => esc_html__( 'My Plugin', 'text-domain' ), 'slug' => 'myplugin' );
    $category_slugs = gc_list_pluck( $categories, 'slug' );

    if ( ! in_array( $block_category['slug'], $category_slugs, true ) ) {
        $categories = array_merge(
            $categories,
            array(
                array(
                    'title' => $block_category['title'], // Required
                    'slug'  => $block_category['slug'], // Required
                    'icon'  => 'gechiui', // Slug of a GeChiUI Dashicon or custom SVG
                ),
            )
        );
    }

    return $categories;
}
add_filter( 'block_categories', 'gcdocs_new_block_category' );

/**
 * Create a custom block category for posts only.
 *
 * @param   array $categories     List of block categories.
 * @return  array
 */
function gcdocs_post_block_category( $categories, $editor_context ) {
	if ( $editor_context->post instanceof GC_Post && 'post' === $editor_context->post->post_type ) {
		$categories = array_merge(
			$categories,
			array(
				'title' => __( 'Title' ), // Required
				'slug'  => 'slug', // Required
				'icon'  => 'gechiui', // Slug of a GeChiUI Dashicon or custom SVG
			)
		);
	}

	return $categories;
}
add_filter( 'block_categories_all', 'gcdocs_post_block_category' );