When You Need Blocks

The following posts are coming up!

Recent Posts



There are a lot of cases where you don’t need a custom block. My spoiler block (from last week) is a great example of that:

But. What about when you want to have that spoiler have choices? Now you’re outside the shared content block territory and into the confusing land of Gutenberg plugins.

Blocks Are Not Simple

I’m the sort of person who abhors boilerplate plugins. I just do. I feel that they divorce you from understanding what the code is and does, and how it all plays together. The more you rely on them, the less you can debug when it goes sideways. Oh, and it will. It always does.

But. Gutenberg requires a lot more Javascript than I happen to be good at. Or rather, it requires new Javascript. Which is why I did exactly what I did the first time I ever made any plugin. I went and found one that did mostly what I wanted, and I edited it.

Whaaaaat? 

It’s true. And I want everyone to take a moment and realize this is a perfectly normal, okay, thing! In fact, I would like to see Hello Dolly converted to include a Gutenblock because learning by example is a valid, tried and true, way to teach someone how things come together.

What Examples Are Good?

Okay so where is a good place to start?

That’s enough to get you started, I think. I went with WP-CLI since I like it and I’m used to it. Of course, it came with a small problem. In order to make a block via wp-cli, you have to put the code in a plugin (or a theme). Well fine. I did that to scaffold it and then I moved it to where I was developing. Most people aren’t so particular.

Is It Hard?

Not really. Once you get used to editing the js file instead of PHP, it’s just like hacking any other plugin. Start with the simple thing. Make a block that just does one thing and isn’t editable. My one tip is that when you call your blocks, make sure to wrap it in a check if the function exists:

if ( function_exists( 'register_block_type' ) ) {
	include_once( dirname( __FILE__ ) . '/spoilers.php' );
}

Otherwise there’s a chance that a non-Gutensite will die on you.

Good luck and happy Gutenberging!


Posted

in

by

%d bloggers like this: