Why is the WordPress Block code so verbose and inefficient?
-
My background is in developing web apps with React, Tailwind, and MUI, but I’ve recently been fiddling with WP for some simpler sites.
I played around with the Gutenberg block editor with this prebuilt Pattern from the 2024 theme:
But looking at the block and rendered code, I had some questions/concerns:
- Why is WP’s block code so verbose and wordy?
To recreate the above pattern design in Tailwind, for example, I’d just do this:
<div class="bg-accent p-16 lg:p-100"> <div class="sp-40 flex flex-wrap"> <div class="flex flex-col justify-between lg:basis-6/12"> <h1 class="font-cardo text-40"> études offers comprehensive consulting, management, design, and research solutions. Every architectural endeavor is an opportunity to shape the future. </h1> <div> <div class="lg:w-6/12"> <p class="mb-20 text-16"> Leaving an indelible mark on the landscape of tomorrow. </p> <a class="font-500 rounded-6 bg-black px-16 py-10 text-white" href="#" > About Us </a> </div> </div> </div> <div class="lg:basis-6/12"> <img src="https://s0.wp.com/wp-content/themes/pub/twentytwentyfour/assets/images/museum.webp" alt="A ramp along a curved wall in the Kiasma Museu, Helsinki, Finland" class="rounded-24" /> </div> </div> </div>
But with WP, the code is 3.5x longer:
<!-- wp:group {"align":"full","style":{"spacing":{"margin":{"top":"0","bottom":"0"},"padding":{"top":"var:preset|spacing|50","bottom":"var:preset|spacing|50","left":"var:preset|spacing|50","right":"var:preset|spacing|50"}}},"backgroundColor":"accent","layout":{"type":"constrained"}} --> <div class="wp-block-group alignfull has-accent-background-color has-background" style=" margin-top: 0; margin-bottom: 0; padding-top: var(--wp--preset--spacing--50); padding-right: var(--wp--preset--spacing--50); padding-bottom: var(--wp--preset--spacing--50); padding-left: var(--wp--preset--spacing--50); " > <!-- wp:columns {"align":"wide","style":{"spacing":{"blockGap":{"top":"var:preset|spacing|40","left":"var:preset|spacing|50"}}}} --> <div class="wp-block-columns alignwide"> <!-- wp:column {"verticalAlignment":"stretch","width":"50%"} --> <div class="wp-block-column is-vertically-aligned-stretch" style="flex-basis: 50%" > <!-- wp:group {"style":{"dimensions":{"minHeight":"100%"}},"layout":{"type":"flex","orientation":"vertical","justifyContent":"stretch","verticalAlignment":"space-between"}} --> <div class="wp-block-group" style="min-height: 100%"> <!-- wp:paragraph {"style":{"typography":{"lineHeight":"1.2"}},"fontSize":"x-large","fontFamily":"heading"} --> <p class="has-heading-font-family has-x-large-font-size" style="line-height: 1.2" > études offers comprehensive consulting, management, design, and research solutions. Every architectural endeavor is an opportunity to shape the future. </p> <!-- /wp:paragraph --> <!-- wp:group {"layout":{"type":"constrained","contentSize":"300px","justifyContent":"left"}} --> <div class="wp-block-group"> <!-- wp:paragraph {"style":{"layout":{"selfStretch":"fixed","flexSize":"50%"}}} --> <p>Leaving an indelible mark on the landscape of tomorrow.</p> <!-- /wp:paragraph --> <!-- wp:buttons --> <div class="wp-block-buttons"> <!-- wp:button --> <div class="wp-block-button"> <a class="wp-block-button__link wp-element-button">About us</a> </div> <!-- /wp:button --> </div> <!-- /wp:buttons --> </div> <!-- /wp:group --> </div> <!-- /wp:group --> </div> <!-- /wp:column --> <!-- wp:column {"verticalAlignment":"center","width":"50%"} --> <div class="wp-block-column is-vertically-aligned-center" style="flex-basis: 50%" > <!-- wp:image {"aspectRatio":"3/4","scale":"cover","sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} --> <figure class="wp-block-image size-large is-style-rounded"> <img src="https://s0.wp.com/wp-content/themes/pub/twentytwentyfour/assets/images/museum.webp" alt="A ramp along a curved wall in the Kiasma Museu, Helsinki, Finland" style="aspect-ratio: 3/4; object-fit: cover" /> </figure> <!-- /wp:image --> </div> <!-- /wp:column --> </div> <!-- /wp:columns --> </div> <!-- /wp:group -->
Is there a particular benefit to this? Because from what I know, more brief and concise code means: faster to write, less network bytes consumed, efficient browser parsing/rendering/reflow performance, easier to read/maintain, more productivity, and better Developer Experience (DX).
2. Why do styles need to be written twice?
Many styles are duplicated first in the WP block comment, and then in the actual tag itself. Why? Can’t we just write Tailwind-like styles (e.g.
class="p-40"
) and have the WP editor parse and display a 40px padding in the UI inputs so the user can easily see/edit it?3. Why are the naming schemes confusing and non-standard?
There are dozens of examples of this, but to illustrate, you see this in the above code:
<!-- wp:group {"style":{"dimensions":{"minHeight":"100%"}},"layout":{"type":"flex","orientation":"vertical","justifyContent":"stretch","verticalAlignment":"space-between"}} -->
CSS flexbox’s already have a defined naming standard for
flex-direction: row | column
. Why not use that instead of"orientation":"vertical"
? But what’s worse, in the rendered style, it’s actuallyjustify-content: space-between
andalign-items: stretch
, the exact opposite of what the block code implies (forjustifyContent
andverticalAlignment
) due to flex direction change, which is unnecessarily confusing.4. What about screen-size-specific styles?
Tailwind has a great way for specifying different styles for different media breakpoints (e.g.
text-20 lg:text-40
). I couldn’t find a way of specifying such styles in the WP editor so the user can easily edit them.Also, more complicated styles not supported by the WP editor (e.g. animations, transforms, clip-paths, background gradients/images, etc.) have to be added to Blocks with CSS class names I think, which are then harder for normal users to identify and remove one if they want to.
I’m not a WP expert, so excuse me if the above concerns are obvious, but any of your feedback or explanations regarding this will be much appreciated.
Thanks in advance…
- The topic ‘Why is the WordPress Block code so verbose and inefficient?’ is closed to new replies.