In working with WordPress Gutenberg blocks, I have added custom styles to existing blocks – for instance, a boxed version of the blockquote – using block filters (register_block_style()
). This is neat because it lets editors click through the visual editor without programming knowledge which secretly adds custom classes.
However, I have a dependency: if the class is-style-box-quote
has been added, small-content-width
must also be present.
Is there any way to add it programmatically? Perhaps when saving the post? Which hook/filter do I use for that?
(After some research and trying)
If I get the part I want to edit via save_post()
(and then $post['post_content']) I get a long HTML string. Like so:
[…]
<!-- wp:quote {"className":"is-style-box-quote"} -->
<blockquote class="wp-block-quote is-style-box-quote"><p>Lorem ipsum dolor sit amet.</p></blockquote>
<!-- /wp:quote -->
[…]
Modifying that via regex would be possible, but a huge pain in the neck. Is there an established preferable way to update the classes in the DB?