Building a WordPress block theme is shortening my life expectancy

Throughout my (now quite long) career as a web developer, I have had many ups and downs. But I have never had as many stomach aches as I have in the last year, trying to wrap my brain around a steady stream of confusing, convoluted, counterintuitive and just downright inexplicable elements of building a theme from the ground up to work with Gutenberg, a.k.a. the WordPress Block Editor.

I’ve made some major progress over the course of the year, and my theme (in various stages of completion) is now powering multiple live client sites, with development ongoing for a few more. For the most part, now, I am finally at a stage where I feel like this is the right way to use WordPress going forward, rather than just reluctantly accepting that I have no choice in the matter.

But I still feel like Gutenberg does a lot of things the wrong way — most notably in its fundamental lack of separation between content and presentation. Yes, I am going to Old Man Yells At Cloud this. I know React is the new hotness and now everything needs to be done in ES6 (which I will forever call JavaScript), but the WordPress core team is throwing away some of the platform’s greatest strengths by abandoning this core component of how it (like pretty much any 2000s-era CMS) is built.

It may seem that I’m just an old curmudgeon who doesn’t want to learn React (I don’t), but it’s not just that. It’s that every aspect of this interface that has been designed to make it easier for average users to interact with — which, I think, it finally is, 5 years after it was unceremoniously forced on us — makes the process of developing for it harder, and more abstract.

There are two unrelated but connected problems with how things are going down here.

First, modern developers just love dependencies.

I get it. To an extent. Reusing tried-and-true code libraries instead of rolling your own all the time is smart. But that means you’re using code you probably haven’t looked at closely. You don’t really know how it works. It may have bugs, or it may have opaque features you don’t realize are there, or it may just have too much stuff bloating it, slowing down performance and making applications more brittle. Pile dozens of these dependencies together, and you’ve got a lumbering behemoth of code that no one in the world completely understands. And I truly do believe we are at a point where no one, at all, knows entirely how the current version of WordPress works. On top of that, any time you’ve got external dependencies, weird things can happen.

Second, Gutenberg is evolving so quickly that the documentation hasn’t kept up.

Gutenberg’s documentation is occasionally out-of-date, always incomplete, and it’s only getting worse.

I know writing documentation is tedious, and the web has never had good documentation. When I was in college in the mid-’90s and I wanted to learn HTML, I went to the campus computing department and asked them how I could go about learning HTML. They, seriously, just told me to download BBEdit. Which I did. And which didn’t help me learn HTML at all. (Although it is still the text editor I write all my code in, 29 years later.) So how did I learn HTML? View source. Because back then, you could do that.

I don’t expect to be able to just “view source” and learn how Gutenberg works. But since WordPress is open source software, and I have the files right here on my computer, I do think that when the documentation fails me, I should be able to poke around in the source code and find what I’m looking for.

Let’s get specific. As it happens, in my theme I’m addressing one of my earlier complaints above by using ACF Blocks. It’s been a rough road, but I’m starting to make really good progress. The only problem is, my styles aren’t getting applied. My CSS for the block isn’t loading in the Block Editor, and the Block Editor styles I’ve configured my block to support (colors, spacing, typography) via its block.json file are not showing up on the front end.

Well, what do I do about that?

Focusing on the front end first, I know that Gutenberg’s styles get applied courtesy of pithy CSS class names like .has-primary-background-color as well as HTML style attributes using CSS variables, like var(--wp--preset--spacing--80).

But if I look at the block attributes in the Block Editor’s comment tag, or the JSON or PHP array of style properties for the block, I see the same is formatted as such: var:preset|spacing|80

The quarter century of development experience in my old fart brain tells me that there must be a function or method, somewhere, that converts var:preset|spacing|80 into var(--wp--preset--spacing--80), and that I would be better off trying to find that function than writing my own.

Uhhh… OK. So how do I go about that?

ACF’s documentation for this feature is abysmal, which is kind of understandable, since the whole thing is a moving target that is changing rapidly (not to mention the organizational challenges that happen when the company that bought you out gets bought out itself), and the core WordPress documentation isn’t much better. So I’m left resorting to a scavenger hunt through the WordPress code. But it’s layer upon layer of 5-line functions referencing each other through a series of add_filter() callbacks.

As usual with my Gutenberg rants, I don’t have any solid conclusions to end on here. This whole post was mostly an exercise in working out that knot that was gnawing at my insides. At least I’ve done that. But I’m no closer to solving my problem. That’s probably because the real problem isn’t what I think it is. And it’s not going away.

All I know is, building a block theme — at least, for me, right now — takes way longer than building a classic theme. And I think that’s because my approach, one developer just cranking away, is not the model anyone in the core WordPress development community cares about, or possibly even comprehends existing. These days I’m not extremely confident about its continued viability myself.


Post script: I think I actually managed to find it, by using BBEdit’s multi-file search on this string: '|'

The method is: WP_Theme_JSON::get_property_value()

Of course, that doesn’t get called directly. It’s called in WP_Theme_JSON::compute_style_properties() which is in turn called by WP_Theme_JSON::get_block_classes() which itself is in turn called by WP_Theme_JSON::get_stylesheet() and then we’re getting too far afield, because that’s used to turn the theme.json file into inline CSS.

So I am guessing at this point that I probably should not use any of these methods. (Actually, I can’t because they’re all protected.) It really seems like this should be happening automatically, and either ACF Blocks are missing some key functionality, or I’m missing something about how ACF Blocks work (which I would blame on the lack of documentation).


Update (April 11, 2023): After I wrote this blog post, I also started a thread on the ACF forum, and there I was finally given an answer. Yes, there is a WordPress function for getting block wrapper attributes. It is even, um, named exactly what it should be. But as the fellow ACF user who responded with that enlightening bit of information even noted, it is incredibly difficult to find. The WordPress documentation does, to its credit, include most if not all (I mean, how would I actually know?) functions and methods, but the search tool is a joke.

Google is no longer a search engine

This is old news, but it’s a useful demonstration of what absolute garbage Google has become as a search engine. It is now an ad engine.

The scenario: I need to set up WordPress Multisite. I’ve done this several times, but since I only have to do the initial setup once every 2-3 years, it’s not something I have memorized. So… I google it! That’s what you do in the 21st century.

So, I went to Google and typed:

'WordPress Multisite installation' Google search

Now, the real solution to this that a smart search engine, which was designed for maximum usefulness as a search engine, would be to provide a link to the official WordPress documentation on the topic.

Is that what it returned? Of course not, silly! It returned four ads, which, depending on your window size, could take up the entire screen:

Google ad results
But then, the first “organic” result should be the official documentation, right?

Wrong!

The first organic result is a page from the dreadful wpbeginner.com, which is overflowing with the most verbose, poorly written, surface-level articles that are designed not to be genuinely useful but to ensure that Google’s search algorithm places them exactly where it did in these results.

Yes, of course, I did click the wpbeginner.com link, because I always do, and then I get annoyed with myself for falling into their trap. And multilingualpress.org is not much better… and also always near the top of the results.

Then, of course, before we finally get to the page I really was looking for, Google makes one last ditch effort to keep me from going where I want to go, by inserting its “People also ask” block, with quick answers scraped from real websites, designed specifically to keep you from actually venturing any deeper than Google’s search results page itself.

Thanks Google for doing your part to make the Internet suck.

P.S. What do you think happened when I clicked “I’m Feeling Lucky”?

I think I just had my first encounter with web push notifications and I HATE IT

What. The. F&#*.

I’ve just spent the past few minutes looking at several restaurants’ pages on OpenTable — because the restaurant company is one of my clients and I’m in the process of updating OpenTable links on their websites.

Each time I kept one of their OpenTable pages open for a minute or so, on my Mac, I would get a “Time Sensitive” notification pop-up on my iPad and my Apple Watch. I realized after the second or third time that some system had determined that it was time for me to leave if I was going to get to the restaurant in time for the reservation I hadn’t even made.

WHY IS THIS HAPPENING.

I would get it if I was heavily into the OpenTable “ecosystem,” but I don’t even have their app installed on any of my devices. I’m guessing it’s this new web push notifications thing. But… GAAAH!! I’m not even logged into OpenTable in my browser on any of these devices, or on my computer. Somehow Safari itself must be pushing these notifications to my devices.

DO NOT WANT.

Gruber thinks Apple is doing this to comply with increased regulatory scrutiny in the E.U. If that’s the case, it just once again proves that the only times governments seem to get involved in the dealings of tech companies is to make things worse, mainly because the people in government making these decisions don’t understand what the problem is, and they understand even less how to fix it. And don’t get me wrong, I am not some kind of techno-libertarian. I think tech companies do a lot of bad things that the government needs to regulate. But the people in government trying to do that just don’t get it.

I can tell you one thing. I absolutely do not want any website I visit, ever, to be able to send push notifications to my f&#*ing devices. Stop. Just stop.

Quick CSS fix for WordPress Block Editor (Gutenberg) link hover color issue

The WordPress Block Editor (a.k.a. Gutenberg) makes it easy to set the text, background, and link colors on any block. But links can and often do have more than one color. And there’s no option here for setting the hover color. So what do you do in what, I think, may be a common situation, where you’re setting a background color on a block and making the link text white, but your theme’s link hover color is either the background color you’re switching to, or something way too close to it?

I’ve come up with a very tidy bit of CSS code that will make your link hover state match the custom link color — granted, you lose the UX of a unique color on hover state, but you gain necessary legibility and accessibility, which I guarantee is more important.

This may not work in every situation, but it’s so simple that it’s worth investigating as an option. With this code, any time you have a block that sets both a custom background color and a custom link color, it will ensure that the hover/focus state matches the custom link color:

main .has-background-color.has-link-color a:focus,
main .has-background-color.has-link-color a:hover
{ color: inherit; }


Update (April 4, 2023): Yeah, don’t do this. You can specify these colors in theme.json. I’m not sure if this is a recent addition, the documentation was previously lacking, or I just didn’t find it, but anyway… do this instead.