{"id":1931,"date":"2020-12-22T21:48:17","date_gmt":"2020-12-22T14:48:17","guid":{"rendered":"https:\/\/wptips.dev\/?p=1931"},"modified":"2020-12-22T22:52:13","modified_gmt":"2020-12-22T15:52:13","slug":"gutenberg-custom-styles","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/gutenberg-custom-styles\/","title":{"rendered":"How to Create Custom Block Styles (Advanced Way)"},"content":{"rendered":"\n<p>Gutenberg at its core lacks features, but its <strong>simplicity is second to none<\/strong> compared to other WordPress page builders.<\/p>\n\n\n\n<p>One way to cover that flaw is by utilizing custom block styles. This is powerful and easy to implement, yet most themes are not using this properly.<\/p>\n\n\n\n<p>In this tutorial, we will cover how to create custom block styles and showcase how it can be used for a more complex design.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1 &#8211; List What You Need<\/h2>\n\n\n\n<p>For example, we need to replicate this design:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"320\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/extend-core-custom-style2-1.jpg\" alt=\"\" class=\"wp-image-1930\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/extend-core-custom-style2-1.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/extend-core-custom-style2-1-480x205.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>According to that, let&#8217;s break down what Gutenberg doesn&#8217;t have yet:<\/p>\n\n\n\n<ol><li><strong>Media &amp; Text<\/strong> block but with a square background behind the image.<\/li><li><strong>Heading<\/strong> block with five-stars decoration.<\/li><li><strong>Button<\/strong> block with only border and arrow pointing to the right.<\/li><\/ol>\n\n\n\n<p>So we need 3 custom styles, let&#8217;s register them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2 &#8211; Register Custom Styles<\/h2>\n\n\n\n<p>The code to add them is quite simple:<\/p>\n\n\n\n<pre title=\"functions.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action(&nbsp;'after_setup_theme',&nbsp;'my_register_block_styles'&nbsp;);\n\n<em>function<\/em>&nbsp;my_register_block_styles()&nbsp;{\n&nbsp;&nbsp;register_block_style(&nbsp;'core\/media-text',&nbsp;[\n&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;=&gt;&nbsp;'half-bg',\n&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;=&gt;&nbsp;__(&nbsp;'Half&nbsp;Background'&nbsp;),\n&nbsp;&nbsp;]&nbsp;);\n \n&nbsp;&nbsp;register_block_style(&nbsp;'core\/heading',&nbsp;[\n&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;=&gt;&nbsp;'stars',\n&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;=&gt;&nbsp;__(&nbsp;'Stars'&nbsp;),\n&nbsp;&nbsp;]&nbsp;);\n\n&nbsp;&nbsp;register_block_style(&nbsp;'core\/button',&nbsp;[\n&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;=&gt;&nbsp;'outline-arrow',\n&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;=&gt;&nbsp;__(&nbsp;'Outline&nbsp;Arrow'&nbsp;),\n&nbsp;&nbsp;]&nbsp;);\n}<\/code><\/pre>\n\n\n\n<p>Now when you create the respective block, you will see the <strong>Styles<\/strong> option on Sidebar:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"470\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/gutenberg-tips-style-inspector.jpg\" alt=\"\" class=\"wp-image-1949\"\/><figcaption>Gutenberg&#8217;s style option<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3 &#8211; Output Color Palette into CSS Variables<\/h2>\n\n\n\n<p>When we select a color in Gutenberg, it will apply extra classes to the wrapper. If the color name is <code>accent<\/code>, then it will add <code>has-accent-background-color<\/code> class.<\/p>\n\n\n\n<p>How they handle that class name is up to the theme. Most of them do it like this:<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-wrong\"><code lang=\"css\" class=\"language-css\">.has-accent-background-color {\n  background-color: #cd2653;\n}\n\n.has-accent-color {\n  color: #cd2653;\n}<\/code><\/pre>\n\n\n\n<p>The problem with the above is that the color is <strong>locked to that property<\/strong>. You can&#8217;t use it for something else like a border or in a <code>:before<\/code> pseudoselector.<\/p>\n\n\n\n<p>It&#8217;s <strong>much more flexible<\/strong> if you output it as CSS Variables instead:<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-correct\"><code lang=\"css\" class=\"language-css\">.has-accent-background-color {\n  --bgColor: #cd2653;\n}\n\n.has-accent-color {\n  --textColor: #cd2653;\n}<\/code><\/pre>\n\n\n\n<p>To output those classes automatically, use the snippet below. It will read the colors defined in <code><a rel=\"noreferrer noopener\" href=\"https:\/\/developer.wordpress.org\/block-editor\/developers\/themes\/theme-support\/\" target=\"_blank\">editor-color-palette<\/a><\/code> and echo it inside <code>&lt;head&gt;<\/code>:<\/p>\n\n\n\n<pre title=\"functions.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action(&nbsp;'wp_head',&nbsp;'my_output_gutenberg_palette'&nbsp;);\nadd_action(&nbsp;'admin_head',&nbsp;'my_output_gutenberg_palette'&nbsp;);\n\n<em>function<\/em>&nbsp;my_output_gutenberg_palette()&nbsp;{\n&nbsp;&nbsp;\/\/&nbsp;abort&nbsp;if&nbsp;in&nbsp;Admin&nbsp;but&nbsp;not&nbsp;inside&nbsp;Gutenberg&nbsp;editor\n&nbsp;&nbsp;if(&nbsp;is_admin()&nbsp;)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;$current_screen;\n&nbsp;&nbsp;&nbsp;&nbsp;$in_editor&nbsp;=&nbsp;method_exists($current_screen,&nbsp;'is_block_editor')&nbsp;&amp;&amp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$current_screen-&gt;is_block_editor();\n&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;!$in_editor&nbsp;)&nbsp;{&nbsp;return;&nbsp;}\n&nbsp;&nbsp;}\n\n&nbsp;&nbsp;$palette&nbsp;=&nbsp;get_theme_support(&nbsp;'editor-color-palette'&nbsp;);\n&nbsp;&nbsp;if(&nbsp;!$palette&nbsp;)&nbsp;{&nbsp;return;&nbsp;}&nbsp;\/\/&nbsp;abort&nbsp;if&nbsp;no&nbsp;palette\n&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;\/\/&nbsp;format&nbsp;styles\n&nbsp;&nbsp;$styles&nbsp;=&nbsp;\":root&nbsp;.has-background {&nbsp;background-color:&nbsp;var(--bgColor);&nbsp;}\n&nbsp;&nbsp;:root&nbsp;.has-text-color&nbsp;{&nbsp;color:&nbsp;var(--textColor);&nbsp;}&nbsp;\";\n\n&nbsp;&nbsp;foreach(&nbsp;$palette[0]&nbsp;as&nbsp;$name&nbsp;=&gt;&nbsp;$value&nbsp;)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;$slug&nbsp;=&nbsp;$value['slug'];\n&nbsp;&nbsp;&nbsp;&nbsp;$color&nbsp;=&nbsp;$value['color'];\n\n&nbsp;&nbsp;&nbsp;&nbsp;$styles&nbsp;.=&nbsp;\".has-{$slug}-background-color&nbsp;{&nbsp;--bgColor:&nbsp;{$color};&nbsp;}&nbsp;\";\n&nbsp;&nbsp;&nbsp;&nbsp;$styles&nbsp;.=&nbsp;\".has-{$slug}-color&nbsp;{&nbsp;--textColor:&nbsp;{$color};&nbsp;}&nbsp;\";\n&nbsp;&nbsp;}\n\n&nbsp;&nbsp;echo&nbsp;\"&lt;style&gt;&nbsp;$styles&nbsp;&lt;\/style&gt;\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<p>With those variables ready, we can <strong>re-purpose the color<\/strong> such as using Heading&#8217;s background as the star&#8217;s color instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">.is-style-stars {\n  background-color: transparent !important;\n}\n\n.is-style-stars::before {\n  content: \"\u2605\u2605\u2605\u2605\u2605\";\n  color: var(--bgColor);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4 &#8211; Write CSS for Admin<\/h2>\n\n\n\n<p>One important tip for this is <strong>to be lazy<\/strong>. No need to perfectly match the design, as long as it has resemblance, it&#8217;s fine.<\/p>\n\n\n\n<p>Also no need to factor in mobile responsiveness because the admin most likely uses a PC to edit the site anyway.<\/p>\n\n\n\n<p>Now that we have the right mindset, here&#8217;s how to enqueue the CSS to Gutenberg:<\/p>\n\n\n\n<pre title=\"functions.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action(&nbsp;'enqueue_block_editor_assets',&nbsp;'my_editor_assets',&nbsp;100&nbsp;);\n\n<em>function<\/em>&nbsp;my_editor_assets()&nbsp;{\n&nbsp;&nbsp;$css_dir&nbsp;=&nbsp;get_stylesheet_directory_uri()&nbsp;.&nbsp;'\/css';\n&nbsp;&nbsp;\n&nbsp;&nbsp;wp_enqueue_style(&nbsp;'my-editor',&nbsp;$css_dir&nbsp;.&nbsp;'\/my-editor.css',\n    [&nbsp;'wp-edit-blocks'&nbsp;],&nbsp;''&nbsp;);\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-gray-background-color has-background\" href=\"https:\/\/gist.github.com\/hrsetyono\/c4e61d9c0d7481f392ddc159f11f2eac\" target=\"_blank\" rel=\"noreferrer noopener\">View my-editor.css<\/a><\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"400\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/gutenberg-tips-example-admin.jpg\" alt=\"\" class=\"wp-image-1951\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/gutenberg-tips-example-admin.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/12\/gutenberg-tips-example-admin-480x256.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>The editor view of our new section.<br><strong>This will look different depending on the theme.<\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5 &#8211; Write CSS for Frontend<\/h2>\n\n\n\n<p>This is where we should focus our effort in.<\/p>\n\n\n\n<p><strong>Start by copy-pasting the admin CSS<\/strong>. Then tune it according to the theme and finally add mobile responsiveness.<\/p>\n\n\n\n<p>We can put this custom styling in existing CSS or create new one like below:<\/p>\n\n\n\n<pre title=\"functions.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action(&nbsp;'wp_enqueue_scripts',&nbsp;'my_frontend_assets',&nbsp;99&nbsp;);\n\n<em>function<\/em>&nbsp;my_frontend_assets()&nbsp;{\n&nbsp;&nbsp;$css_dir&nbsp;=&nbsp;get_stylesheet_directory_uri()&nbsp;.&nbsp;'\/css';\n\n&nbsp;&nbsp;wp_enqueue_style(&nbsp;'my-style',&nbsp;$css_dir&nbsp;.&nbsp;'\/my-style.css',&nbsp;[]&nbsp;);\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-gray-background-color has-background\" href=\"https:\/\/gist.github.com\/hrsetyono\/c4e61d9c0d7481f392ddc159f11f2eac#file-my-style-css\" target=\"_blank\" rel=\"noreferrer noopener\">View my-style.css<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You might get intimidated by how long the process for creating just 1 section. But remember that Step 3 and the enqueue process are one-time-only.<\/p>\n\n\n\n<p>Once you learn once, it won&#8217;t take long. You&#8217;ll also be surprised at how seamless it is to convert the admin CSS into the frontend.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>If you have any question regarding Custom Block Style, feel free to leave a comment below \ud83d\ude42<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>One way to patch Gutenberg&#8217;s lack of features is to use custom block styles. But most themes are not utilizing it properly. We will learn the proper way here.<\/p>\n","protected":false},"author":1,"featured_media":1974,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[21,22,32],"class_list":["post-1931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gutenberg","tag-css","tag-gutenberg","tag-javascript"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1931"}],"version-history":[{"count":10,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1931\/revisions"}],"predecessor-version":[{"id":1979,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1931\/revisions\/1979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1974"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}