• Resolved kittenchief117

    (@kittenchief117)


    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:

    1. 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 actually justify-content: space-between and align-items: stretch, the exact opposite of what the block code implies (for justifyContent and verticalAlignment) 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…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @kittenchief117. These are good questions and there’s a big answer to them; here’s a not-so-brief summary.

    The most important part to understanding this is probably that WordPress is trying to build an open and extensible system. Most of the decisions related to the way content is stored is directly working to one of those two goals.

    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).

    WordPress posts tend to perform quite well in rendered pages and there’s been a fair amount of care built into the editor in order to produce semantic and efficient HTML. One part to note is that the content stored in a post is not the same content delivered to the browser. In any case, the extra metadata stored with each block is rather minor, and a single image upload to a site might outweigh the total of all the additional data surrounding blocks.

    Those HTML comments provide a very fast way to parse the blocks out of a post and to isolate them for processing on page render without requiring the use of more opaque data structures like a proprietary JSON format. They provide for sufficient functionality when rendered as an HTML file without any server while communicating the higher-level attributes which define what any given block is and means.

    For a deeper dive into this question, you can read more in an article I wrote many years ago now.

    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…Why not use that instead of?"orientation":"vertical"?

    Duplication should be kept at a minimum, but if you look carefully at the example you posted you will see that there are minor differences. A Block represents a kind of rich media and its attributes define the parameters which define that particular bit. An image block’s url attribute indicates where to find an image file, but the HTML that’s rendered for that block could be one of many different HTML constructions depending on things like whether there are different sizes available for display, whether there’s a caption, whether there are artistic choices being made based on the screen size. Styling is similar in that there are concepts being defined at a higher lever than raw CSS. There’s a close correspondence, but CSS becomes a way to express the higher-level style kind. Therefore most block’s have attributes indicating the intentional styles while the HTML inside a block contains the realization of that style with CSS.

    It’s common when developing blocks to add CSS late during render and to do that based on the block attribute or based on some other information available to the server when rendering a page. If you examine the rendered page you will note, for instance, that most blocks contain a number of additional classes which aren’t stored with the block in the editor or in the database. WordPress provides the render_block filter to modify the HTML when sending a page to a browser.

    Also, more complicated styles not supported by the WP editor…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.

    If you find that you are adding things routinely to many blocks then you might look into that render_block filter and see if it’s something that you can provide dynamically.

    Many of the style systems are designed to work together with Global Styles and block supports that give people the ability to adjust the layout or visual styling of a block or page without resorting to code or CSS. This is also one of the reasons it’s hard to rely entirely on CSS, because the WordPress concepts often transcend an individual block in a post or an individual setting.

    Everything being built is done in the open and in a spirit of cooperation and invitation. If you experience specific problems or want to share an idea for how to do things differently, you can interact on the Issues page for Gutenberg (the name of the editor).

    Thanks for asking!

    Thread Starter kittenchief117

    (@kittenchief117)

    Hi @dmsnell,

    Thank you for taking the time to write the detailed response! I appreciate it.

    I understand and agree with you for the most part. I guess I’m used to having a modern JS dev environment with automatic type-checking, completions, linting, prettifying, highlighting, and many other dev-friendly features, that developing on WordPress still seems a bit cumbersome.

    Regarding point #1: Yes, you’re right that other assets will be much larger than the block code, but those assets can also often be lazy-loaded separately and aren’t as critical for UX and Core Web Vitals (FCP, first-byte) as the initial HTML page, which loads the main content the user is interested in.

    But even more importantly, it takes much longer to write the code! I love utility-style class names for how quickly and efficiently you can build layouts, and I wish WP had them too (side note: for my sanity I might even write a script that takes my Tailwind HTML code and auto-converts it to WP block code wherever possible!)

    Of course, if a block has a property that isn’t directly correlated/mapped to a class name or CSS style, it can go in the block comment (or perhaps a better structure as the block comment JSON isn’t easily prettified, autocompleted, or syntax highlighted by code editors).

    But I believe the majority of the styles are duplicated, which can add unnecessary confusion, extra learning curve, weird bugs, and more things to remember, especially because of the non-standard naming schemes.

    I’ll also try looking into other editors like Elementor and Divi to see how things are.

    But either way, thank you again for the answers. Have a great day Dennis…

    Yes, you’re right that other assets will be much larger than the block code, but those assets can also often be lazy-loaded separately and aren’t as critical for UX and Core Web Vitals (FCP, first-byte) as the initial HTML page, which loads the main content the user is interested in.

    In review I could have been clearer in stressing that this duplication isn’t present on the rendered page. The HTML comments and their JSON attributes are stripped away when rendering, styles are merged, and for some blocks the entire content is swapped out when building an actual page.

    What I was trying to convey was that in terms of the delivered page, WordPress and its block content performs quite well. Here’s a 2021 article discussing how block posts score high in the browser for performance and for mobile accessibility.

    But even more importantly, it takes much longer to write the code! I love utility-style class names for how quickly and efficiently you can build layouts, and I wish WP had them too (side note: for my sanity I might even write a script that takes my Tailwind HTML code and auto-converts it to WP block code wherever possible!)

    It’s easy to sympathize with this, but the systems are different and you may find that there are also alternative ways to develop that eliminate that coding altogether.

    For instance, if you haven’t explored them, check out Block Patterns. The combination of creating a block pattern and using the Group Block effectively can be a quick way to build reusable micro-layouts in your posts, and that doesn’t involve any code at all. The benefits of the ecosystem come with it too; for example, when building columns there’s an option to check that transforms columns into a vertical list of each column on mobile displays so that it avoids awkward horizontal scrolling.

    thank you again for the answers. Have a great day

    Glad to try and clarify! You have a great day as well.

    Thread Starter kittenchief117

    (@kittenchief117)

    Yeah, I have been looking into Block Patterns.

    Though, at least personally, I find it much faster to edit the raw code in my code editor (with all the advanced editing capabilities) versus changing things in the UI, but I’ll look into it more.

    Thank you for all the suggestions and help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why is the WordPress Block code so verbose and inefficient?’ is closed to new replies.