Ben
Ben is a lifelong Nintendo fan who likes to build websites, and make video games. He buys way too much Lego.
WordPress and Games
Last week Keith Devon asked if anyone knew how to fix Gutenbergs floated image blocks. For some reason the floated image gets wrapped in a containing div.
I’m not sure why this is done, but the code is right there cherry picking the blocks with an alignment and adding an extra wrapper. This makes floating and positioning content difficult since the positional controls are added inside the container.
Simon Blackbourn suggested using the render_block
filter to tweak the html, and so that’s what I did. The code ended up being very simple, it’s basically just a handful of string replacements.
What the code does is first check if we’re using the Image block. We could simplify this in WP 5.7 with the new render_block_core/image
filter.
We then loop through the possible alignments and check to see if the current block contains one of them. If it does then we first remove the old alignment, then replace the wrapper class with the alignment we removed – shifting the alignment onto the wrapper. Finally I removed any empty class attributes.
The wrapper is added so that certain alignment situations happen, but they also mean that others can’t happen. There’s a full explanation on this Gutenberg Github issue. The Github issue was very active this time last year but has now stalled, which suggests to me that it won’t be addressed; but I hope I’m wrong since improving this would make theming a lot simpler.
I’m now wondering about adding this to Toolbelt along with fixes for any other blocks that have funky rendering behaviours. For example the Pullquote and Blockquote blocks have different wrappers that make styling the two blocks consistently a pain, so maybe something could be done to improve that as well.
Hi Ben, I’d be very interested to know if you do eventually add this to Toolbelt. As a user and fan of the plug-in, I’d love and appreciate seeing this added in the future. Thanks for a useful and sleek plug-in. It replaces a few different plugins I was using.
Thanks for the feedback. It’s great that you find the plugin, and this code useful 🙂
I’m in the middle of other work at the moment, but this is definitely something I could do quite quickly (since I already have the code) when time allows.