The get_default_block_categories() WordPress PHP function returns the list of default categories for block types.
Usage
$default_block_categories = get_default_block_categories(); print_r($default_block_categories);
Output:
Array
(
[0] => Array
(
[slug] => text
[title] => Text
)
[1] => Array
(
[slug] => media
[title] => Media
)
...
)
Parameters
- None
More information
See WordPress Developer Resources: get_default_block_categories()
Examples
Display default block categories as a list
This example fetches the default block categories and displays them as an unordered list.
$categories = get_default_block_categories();
echo "<ul>";
foreach ($categories as $category) {
echo "<li><strong>{$category['title']}</strong> ({$category['slug']})</li>";
}
echo "</ul>";
Check if a specific category exists
This example checks if the ‘text’ category exists in the default block categories.
$categories = get_default_block_categories();
$has_text_category = false;
foreach ($categories as $category) {
if ($category['slug'] == 'text') {
$has_text_category = true;
break;
}
}
echo $has_text_category ? 'Text category exists' : 'Text category not found';
Retrieve a category title by slug
This example retrieves the title of the ‘media’ category by its slug.
$categories = get_default_block_categories();
$media_title = '';
foreach ($categories as $category) {
if ($category['slug'] == 'media') {
$media_title = $category['title'];
break;
}
}
echo $media_title;
Add a custom category to the default block categories
This example adds a custom category ‘custom’ to the default block categories.
$categories = get_default_block_categories();
$custom_category = array(
'slug' => 'custom',
'title' => 'Custom'
);
array_push($categories, $custom_category);
print_r($categories);
Filter default block categories by a search term
This example filters the default block categories by the search term ‘media’.
$search_term = 'media';
$categories = get_default_block_categories();
$filtered_categories = array_filter($categories, function($category) use ($search_term) {
return strpos(strtolower($category['title']), strtolower($search_term)) !== false;
});
print_r($filtered_categories);