{"id":6006,"date":"2020-08-05T17:47:00","date_gmt":"2020-08-05T15:47:00","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=6006"},"modified":"2020-08-05T16:07:42","modified_gmt":"2020-08-05T14:07:42","slug":"block-edit-wrapper-attributes","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/block-edit-wrapper-attributes-6006\/","title":{"rendered":"Change block edit wrapper attributes"},"content":{"rendered":"\n<p>In Gutenberg, the markup from the <code>edit<\/code> function is wrapped in a few other elements. The outer wrapper of each block has the <code>wp-block<\/code> class and a <code>data-block<\/code> attribute that contains the name of the block with its namespace \u2013 <code>core\/group<\/code> for the group block, for example.<\/p>\n\n\n\n<p>We can change the attributes of that wrapper, so we could add class names depending on the selected\/changed block options, what could make the styling of blocks in the backend easier.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>A way that works for all blocks is the <code>editor.BlockListBlock<\/code> filter. To add a class name that contains the value of an attribute, we could do the following (<a href=\"https:\/\/wordpress.stackexchange.com\/a\/324095\/112824\">solution highly inspired by a StackExchange answer<\/a>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> { createHigherOrderComponent } = wp.compose;\n<span class=\"hljs-keyword\">const<\/span> withStyleClasses = createHigherOrderComponent( <span class=\"hljs-function\">(<span class=\"hljs-params\"> BlockListBlock <\/span>) =&gt;<\/span> {\n\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-function\">(<span class=\"hljs-params\"> props <\/span>) =&gt;<\/span> {\n\t\t<span class=\"hljs-keyword\">if<\/span> ( props.name !== <span class=\"hljs-string\">'my\/block'<\/span> ) {\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BlockListBlock<\/span> { <span class=\"hljs-attr\">...props<\/span> } \/&gt;<\/span><\/span>\n\t\t}\n\n\t\t<span class=\"hljs-keyword\">const<\/span> {\n\t\t\tmyAttribute,\n\t\t} = props.attributes;\n\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BlockListBlock<\/span> { <span class=\"hljs-attr\">...props<\/span> } <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">{<\/span> `<span class=\"hljs-attr\">my-attribute-<\/span>${ <span class=\"hljs-attr\">myAttribute<\/span> }` } \/&gt;<\/span><\/span>;\n\t};\n}, <span class=\"hljs-string\">'withStyleClasses'<\/span> );\n\nwp.hooks.addFilter( <span class=\"hljs-string\">'editor.BlockListBlock'<\/span>, <span class=\"hljs-string\">'slug\/with-style-classes'<\/span>, withStyleClasses );\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>withStyleClasses<\/code> function, that is hooked to the <code>editor.BlockListBlock<\/code> filter gets the <code>BlockListBlock<\/code> as a parameter. Inside the function, we check if the block identifier is <code>my\/block<\/code>, otherwise we return <code>BlockListBlock<\/code> without any modifications.<\/p>\n\n\n\n<p>If we have the wanted block, we get the value of the <code>myAttribute<\/code> attribute and add a class name that begins with <code>my-attribute-<\/code> followed by the current value of the attribute.<\/p>\n\n\n\n<p><a href=\"https:\/\/wordpress.stackexchange.com\/a\/335020\/112824\">Another way that I found<\/a> works with <code>getEditWrapperProps<\/code> when registering a block, but is, at least in my tests, not usable for things like additional classes (because it did not work to add the class to existing ones but only replace existing ones with the new class). But it seems to be very useful for additional <code>data-<\/code> attributes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Gutenberg, the markup from the edit function is wrapped in a few other elements. The outer wrapper of each block has the wp-block class and a data-block attribute that contains the name of the block with its namespace \u2013 core\/group for the group block, for example. We can change the attributes of that wrapper, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-6006","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":"a903d4b70f214ecca269f86d0ae1b346","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/a903d4b70f214ecca269f86d0ae1b346"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/6006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=6006"}],"version-history":[{"count":3,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/6006\/revisions"}],"predecessor-version":[{"id":6009,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/6006\/revisions\/6009"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=6006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=6006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=6006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}