Possible to change a WordPress block's default text alignment state?

Viewed 22

Very easy to do via css stylesheet, but is it possible to modify the default text alignment in a way that also updates a block's text alignment button? I've been looking through the documentation and searching online, but can't seem to find any relevant functions, options, etc.

Thanks in advance for any help!

1 Answers

A block variation can create a variation of an existing block and modify any of the blocks default attributes like text alignment. Eg. a variation of heading that is center aligned:

JavaScript

wp.blocks.registerBlockVariation('core/heading', {
    name: 'center',
    title: 'Heading: Center',
    attributes: { textAlign: 'center' },
});

The example above sets the text alignment control to center while still enabling the alignment to be changed via the control in the Editor. As its a block variation, the markup generated by the variation is still the same:

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Heading</h2>
<!-- /wp:heading --> 
Related