[{"data":1,"prerenderedAt":356},["ShallowReactive",2],{"\u002Fblog\u002Fchanging-a-gutenberg-block-metadata-during-runtime":3},{"id":4,"title":5,"body":6,"categories":343,"date":345,"description":346,"extension":347,"image":348,"meta":349,"navigation":350,"path":351,"seo":352,"sitemap":353,"stem":354,"tags":348,"__hash__":355},"blog\u002Fblog\u002Fchanging-a-gutenberg-block-metadata-during-runtime.md","Changing a Gutenberg block metadata during runtime",{"type":7,"value":8,"toc":339},"minimark",[9,22,27,33,37,63,217,220,335],[10,11,12,13,17,18,21],"p",{},"I have ran into at least one situation where I needed to modify either an ACF Block or a core Gutenberg block selectively during runtime, but keep the ",[14,15,16],"code",{},"blocks.json"," structure the same since it applied to 90% of my use cases. This could be either removing or adding a supports option at a per post type level, or maybe adding new typography options during on what post type the user is using. Fortunately, this process is pretty easy to do using the ",[14,19,20],{},"block_type_metadata"," filter.",[23,24,26],"h2",{"id":25},"what-blocks-can-be-modified","What blocks can be modified?",[10,28,29,30,32],{},"First, it's important to note that not all blocks in WordPress can be modified during runtime. This only applies to blocks using the new v2 structure and uses the ",[14,31,16],{}," file to setup their metadata. If you are using WordPress 6.0+, then this method will work for any core blocks. If you are using ACF 6+, then this will work for any blocks using the new structure. This may or may not work for third-party plugin blocks.",[23,34,36],{"id":35},"adding-the-filter","Adding the filter",[10,38,39,40,46,47,50,51,54,55,58,59,62],{},"The ",[41,42,20],"a",{"href":43,"rel":44},"https:\u002F\u002Fdeveloper.wordpress.org\u002Freference\u002Fhooks\u002Fblock_type_metadata\u002F",[45],"nofollow"," filter will let you hook into all blocks being processed at that moment and view their metadata as a PHP array. You can use the ",[14,48,49],{},"$metadata['name']"," variable to decide which block you want to target specifically, then use any further Wordpress function to look for items, such as the admin screen page, post type, ",[14,52,53],{},"is_single()",", etc. Below is a simple example of removing the ",[14,56,57],{},"anchor"," and ",[14,60,61],{},"customClassName"," from all blocks.",[64,65,70],"pre",{"className":66,"code":67,"language":68,"meta":69,"style":69},"language-php shiki shiki-themes material-theme-lighter material-theme-palenight","add_filter( 'block_type_metadata', 'remove_the_advanced_options' );\nfunction remove_the_advanced_options($metadata ) {\n    $metadata['supports']['customClassName'] = false;\n    $metadata['supports']['anchor'] = false;\n    return $metadata;\n}\n","php","",[14,71,72,107,130,167,196,211],{"__ignoreMap":69},[73,74,77,81,85,88,91,94,97,99,102,104],"span",{"class":75,"line":76},"line",1,[73,78,80],{"class":79},"sp2xM","add_filter",[73,82,84],{"class":83},"s4Nzc","(",[73,86,87],{"class":83}," '",[73,89,20],{"class":90},"s9-rc",[73,92,93],{"class":83},"'",[73,95,96],{"class":83},",",[73,98,87],{"class":83},[73,100,101],{"class":90},"remove_the_advanced_options",[73,103,93],{"class":83},[73,105,106],{"class":83}," );\n",[73,108,110,114,117,120,124,127],{"class":75,"line":109},2,[73,111,113],{"class":112},"sBv9x","function",[73,115,116],{"class":79}," remove_the_advanced_options",[73,118,119],{"class":83},"($",[73,121,123],{"class":122},"sv0dM","metadata ",[73,125,126],{"class":83},")",[73,128,129],{"class":83}," {\n",[73,131,133,136,139,142,144,147,149,152,154,156,158,161,164],{"class":75,"line":132},3,[73,134,135],{"class":83},"    $",[73,137,138],{"class":122},"metadata",[73,140,141],{"class":83},"[",[73,143,93],{"class":83},[73,145,146],{"class":90},"supports",[73,148,93],{"class":83},[73,150,151],{"class":83},"][",[73,153,93],{"class":83},[73,155,61],{"class":90},[73,157,93],{"class":83},[73,159,160],{"class":83},"]",[73,162,163],{"class":83}," =",[73,165,166],{"class":83}," false;\n",[73,168,170,172,174,176,178,180,182,184,186,188,190,192,194],{"class":75,"line":169},4,[73,171,135],{"class":83},[73,173,138],{"class":122},[73,175,141],{"class":83},[73,177,93],{"class":83},[73,179,146],{"class":90},[73,181,93],{"class":83},[73,183,151],{"class":83},[73,185,93],{"class":83},[73,187,57],{"class":90},[73,189,93],{"class":83},[73,191,160],{"class":83},[73,193,163],{"class":83},[73,195,166],{"class":83},[73,197,199,203,206,208],{"class":75,"line":198},5,[73,200,202],{"class":201},"sNZSE","    return",[73,204,205],{"class":83}," $",[73,207,138],{"class":122},[73,209,210],{"class":83},";\n",[73,212,214],{"class":75,"line":213},6,[73,215,216],{"class":83},"}\n",[10,218,219],{},"Let's say that we only want to limit this for core blocks:",[64,221,223],{"className":66,"code":222,"language":68,"meta":69,"style":69},"if (str_starts_with($metadata['name'], 'core\u002F')) {\n    $metadata['supports']['anchor'] = false;\n    $metadata['supports']['customClassName'] = false;\n}\nreturn $metadata;\n",[14,224,225,264,292,320,324],{"__ignoreMap":69},[73,226,227,230,233,236,238,240,242,244,247,249,252,254,257,259,262],{"class":75,"line":76},[73,228,229],{"class":201},"if",[73,231,232],{"class":83}," (",[73,234,235],{"class":79},"str_starts_with",[73,237,119],{"class":83},[73,239,138],{"class":122},[73,241,141],{"class":83},[73,243,93],{"class":83},[73,245,246],{"class":90},"name",[73,248,93],{"class":83},[73,250,251],{"class":83},"],",[73,253,87],{"class":83},[73,255,256],{"class":90},"core\u002F",[73,258,93],{"class":83},[73,260,261],{"class":83},"))",[73,263,129],{"class":83},[73,265,266,268,270,272,274,276,278,280,282,284,286,288,290],{"class":75,"line":109},[73,267,135],{"class":83},[73,269,138],{"class":122},[73,271,141],{"class":83},[73,273,93],{"class":83},[73,275,146],{"class":90},[73,277,93],{"class":83},[73,279,151],{"class":83},[73,281,93],{"class":83},[73,283,57],{"class":90},[73,285,93],{"class":83},[73,287,160],{"class":83},[73,289,163],{"class":83},[73,291,166],{"class":83},[73,293,294,296,298,300,302,304,306,308,310,312,314,316,318],{"class":75,"line":132},[73,295,135],{"class":83},[73,297,138],{"class":122},[73,299,141],{"class":83},[73,301,93],{"class":83},[73,303,146],{"class":90},[73,305,93],{"class":83},[73,307,151],{"class":83},[73,309,93],{"class":83},[73,311,61],{"class":90},[73,313,93],{"class":83},[73,315,160],{"class":83},[73,317,163],{"class":83},[73,319,166],{"class":83},[73,321,322],{"class":75,"line":169},[73,323,216],{"class":83},[73,325,326,329,331,333],{"class":75,"line":198},[73,327,328],{"class":201},"return",[73,330,205],{"class":83},[73,332,138],{"class":122},[73,334,210],{"class":83},[336,337,338],"style",{},"html pre.shiki code .sp2xM, html code.shiki .sp2xM{--shiki-default:#6182B8;--shiki-dark:#82AAFF}html pre.shiki code .s4Nzc, html code.shiki .s4Nzc{--shiki-default:#39ADB5;--shiki-dark:#89DDFF}html pre.shiki code .s9-rc, html code.shiki .s9-rc{--shiki-default:#91B859;--shiki-dark:#C3E88D}html pre.shiki code .sBv9x, html code.shiki .sBv9x{--shiki-default:#9C3EDA;--shiki-dark:#C792EA}html pre.shiki code .sv0dM, html code.shiki .sv0dM{--shiki-default:#90A4AE;--shiki-dark:#BABED8}html pre.shiki code .sNZSE, html code.shiki .sNZSE{--shiki-default:#39ADB5;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":69,"searchDepth":109,"depth":132,"links":340},[341,342],{"id":25,"depth":109,"text":26},{"id":35,"depth":109,"text":36},[344],"WordPress","2022-09-30","Manipulating Gutenberg blocks at runtime is easy and sometimes necessary. Let's give it a try with the block_type_metadata filter.","md",null,{},true,"\u002Fblog\u002Fchanging-a-gutenberg-block-metadata-during-runtime",{"title":5,"description":346},{"loc":351,"lastmod":345},"blog\u002Fchanging-a-gutenberg-block-metadata-during-runtime","tELucL8oRjcTnrK46vEaL5SNzqQG3uYXGBbZAdJnVpI",1786138781024]