Beim Einfügen von Blöcken befinden sich diese in verschiedenen Kategorien wie Design, Widgets, Embeds, oder auch von Plugins beigesteuerte Gruppen (und andersfarbigen Icons). Es bietet sich an, für eigene Blöcke eine eigene Kategorie anzulegen, z.B. „Child-Theme“, oder Name des eigenen Plugins.
/**
* Creating a custom block category.
* @param array $categories - List of block categories.
* @return array
*/
function childtheme_new_block_category( $categories ) {
$block_category = array( 'title' => esc_html__( 'My Custom Plugin', 'custom-plugin-textdomain' ), 'slug' => 'myplugin' );
$category_slugs = wp_list_pluck( $categories, 'slug' );
if ( ! in_array( $block_category['slug'], $category_slugs, true ) ) {
$categories = array_merge(
$categories,
array(
array(
'title' => $block_category['title'], // erforderlich
'slug' => $block_category['slug'], // erforderlich
'icon' => 'wordpress', // optional: Dashicon slug oder SVG
),
)
);
}
return $categories;
}
add_filter( 'block_categories', 'childtheme_new_block_category' );
Nachtrag August: Mit WordPress 5.8 (Tatum, erschienen am 20. Juli 2021) wird block_categories
als deprecated angezeigt. stattdessen muss man block_categories_all
verwenden.
/**
* Creating a custom block category.
* @param array $categories - List of block categories.
* @return array
*/
function childtheme_new_block_category( $categories ) {
$block_category = array( 'title' => esc_html__( 'My Custom Plugin', 'custom-plugin-textdomain' ), 'slug' => 'myplugin' );
$category_slugs = wp_list_pluck( $categories, 'slug' );
if ( ! in_array( $block_category['slug'], $category_slugs, true ) ) {
$categories = array_merge(
array(
array(
'title' => $block_category['title'], // erforderlich
'slug' => $block_category['slug'], // erforderlich
'icon' => 'wordpress', // optional: Dashicon slug oder SVG
),
),
$categories
);
}
return $categories;
}
add_filter( 'block_categories_all', 'childtheme_new_block_category' );
Beispiel (in diesem steht die eigene Kategorie an 1. Stelle)
Schreibe einen Kommentar