{"id":1293,"date":"2024-06-11T06:24:16","date_gmt":"2024-06-11T04:24:16","guid":{"rendered":"https:\/\/wypo.io\/creer-nouvelle-categorie-blocs-gutenberg\/"},"modified":"2024-07-18T23:19:48","modified_gmt":"2024-07-18T21:19:48","slug":"create-new-category-gutenberg-blocks","status":"publish","type":"post","link":"https:\/\/wypo.io\/en\/create-new-category-gutenberg-blocks\/","title":{"rendered":"Create a new category for Gutenberg blocks"},"content":{"rendered":"\n<div class=\"wp-block-group   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"\">The block category in Gutenberg is a feature that allows organizing and grouping the various blocks available in the WordPress block editor. It provides a logical and intuitive structure for users when they are searching for and selecting blocks to build their pages or posts.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">\n<h2 class=\"wp-block-heading   \" datalink=\"content-why-create-a-new-category\">Why Create a New Category?<\/h2>\n\n\n\n<p class=\"\">Creating a new category for Gutenberg blocks in WordPress can be interesting for several reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>Organization<\/strong>: Having specific categories for your blocks helps better organize the Gutenberg editor. It makes it easier to find relevant blocks, especially if you have a large number of custom blocks.<\/li>\n\n\n\n<li class=\"\"><strong>Clarity<\/strong>: By creating specific categories, you can provide clearer meaning to the purpose of each block, helping users quickly understand the usefulness of each available block.<\/li>\n\n\n\n<li class=\"\"><strong>Customization<\/strong>: If you&#8217;re developing blocks for a specific theme or plugin, grouping them into a custom category makes the user interface more consistent and aligned with the theme or plugin&#8217;s functionality.<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">\n<h2 class=\"wp-block-heading   \" datalink=\"content-default-block-categories\">Default Block Categories<\/h2>\n\n\n\n<p class=\"\">Gutenberg provides a series of default categories for organizing the available blocks. These categories offer a basic structure for classifying blocks based on their use and content type. Here are some of the most common default categories:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>Text<\/strong><br>The Text category blocks are used for adding and formatting textual content. It includes paragraph, heading, list, quote blocks, etc., which enable composing and styling text effectively.<\/li>\n\n\n\n<li class=\"\"><strong>Media<\/strong><br>The Media category groups blocks that allow adding and manipulating media such as images, galleries, videos, and audio files. These blocks provide a simple and intuitive way to integrate different types of media into content.<\/li>\n\n\n\n<li class=\"\"><strong>Design<\/strong><br>This category groups blocks that help structure the layout of a page or post. It typically includes column, group, and container blocks that facilitate creating complex layouts.<\/li>\n\n\n\n<li class=\"\"><strong>Widgets<\/strong><br>Widgets category blocks are often used to add interactive or dynamic elements to a page or post. It includes blocks like buttons, contact forms, counters, icons, etc., which enrich the user experience.<\/li>\n\n\n\n<li class=\"\"><strong>Theme<\/strong><br>This category groups blocks specific to a given WordPress theme. These blocks are designed to work optimally with the active theme on the WordPress site, providing visual and functional consistency in layout design.<\/li>\n\n\n\n<li class=\"\"><strong>Embedded Content<\/strong><br>This category groups blocks that allow embedding content from external sources such as social networks, video platforms, and mapping services. It includes blocks for embedding tweets, YouTube videos, Google Maps, etc.<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">\n<h2 class=\"wp-block-heading   \" datalink=\"content-adding-the-category-in-php\">Adding the Category in PHP<\/h2>\n\n\n\n<p class=\"\">To create a new Gutenberg block category, you need to use the <code>block_categories<\/code> filter (or <code>block_categories_all<\/code> for newer versions of WordPress) in your plugin or theme file. Here&#8217;s an example code to add a new category:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-1492547893\"\n        id=\"codemirror-1492547893\"\n      >function my_custom_block_categories($categories, $post) {\n  return array_merge(\n      $categories,\n      array(\n          array(\n              &#039;slug&#039; =&gt; &#039;my-custom-category&#039;,\n              &#039;title&#039; =&gt; __(&#039;My Custom Category&#039;, &#039;text-domain&#039;),\n              &#039;icon&#039;  =&gt; &#039;star-filled&#039;, \/\/ Nom de l&#039;ic\u00f4ne Dashicon (facultatif)\n          ),\n      )\n  );\n}\n\nif ( version_compare( get_bloginfo( &#039;version&#039; ), &#039;5.8&#039;, &#039;&gt;=&#039; ) ) {\n\tadd_filter( &#039;block_categories_all&#039;, &#039;my_custom_block_categories&#039; );\n} else {\n\tadd_filter( &#039;block_categories&#039;, &#039;my_custom_block_categories&#039; );\n}<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-1492547893'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n\n\n<p class=\"\">When registering your blocks, you can assign them to the new category by specifying the category in the block configuration object.<\/p>\n\n\n\n<p class=\"\"><strong>Code Explanation<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"\"><strong><code>block_categories_all<\/code> Filter<\/strong> (formerly <code>block_categories<\/code>): This filter allows modifying or adding block categories in the Gutenberg editor. It takes two arguments: the existing categories and the current post.<\/li>\n\n\n\n<li class=\"\"><strong><code>my_custom_block_categories<\/code> Function<\/strong>: This function adds a new category to the list of existing categories. The new category has a slug, a title, and an optional icon.<\/li>\n\n\n\n<li class=\"\"><strong>Block Registration<\/strong>: When registering a new block with <code>registerBlockType<\/code>, you specify the category using the slug of the custom category.<\/li>\n<\/ol>\n\n\n\n<p class=\"\">By following these steps, you can create a new category for your Gutenberg blocks, improving organization and user experience in the editor.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The block category in Gutenberg is a feature that allows organizing and grouping the various blocks available in the WordPress block editor. It provides a logical and intuitive structure for users when they are searching for and selecting blocks to build their pages or posts. Why Create a New Category? Creating a new category for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_habefastfse_class":"","hf_wpseo_meta_title":"Create a category for Gutenberg blocks [separator]\u00a0[sitetitle]\u00a0","hf_wpseo_meta_description":"Discover how to organize your WordPress blocks by creating custom categories for Gutenberg. Follow our step-by-step guide to improve the clarity and customizability of your block editor.","hf_wpseo_meta_robots_index":"","hf_wpseo_meta_robots_follow":true,"hf_wpseo_meta_robots_advanced":"{}","hf_wpseo_meta_canonical_url":"","footnotes":""},"categories":[36],"tags":[],"class_list":["post-1293","post","type-post","status-publish","format-standard","hentry","category-gutenberg"],"_links":{"self":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/comments?post=1293"}],"version-history":[{"count":1,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1293\/revisions"}],"predecessor-version":[{"id":1295,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1293\/revisions\/1295"}],"wp:attachment":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/media?parent=1293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/categories?post=1293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/tags?post=1293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}