Register and Unregister WordPress Block Styles with register_block_style()

Register and remove block styles with register_block_style()

Block styles are variants of a block that editors choose in the “Styles” panel of the block sidebar. Choosing one adds a class is-style-{name} to the block, and the theme’s CSS does the rest. Core has a few of them (Default and Rounded for the image, Fill and Outline for the button, Default and Large for the quote). Since WordPress 5.3 a theme can add its own in PHP, without any JavaScript.

Problem: Editors need a “Framed” image and a “Ghost” button that match the design, and some core styles should not be offered because the theme does not support them.

Solution: Register styles with register_block_style() on init and add CSS for the is-style-* classes, loaded in the editor and on the front end. Remove core styles in JavaScript with wp.blocks.unregisterBlockStyle(), because they are registered in JavaScript.

Registering styles

<?php
add_action( 'init', 'ha_register_block_styles' );

function ha_register_block_styles() {
    register_block_style( 'core/image', array(
        'name'  => 'framed',
        'label' => __( 'Framed', 'textdomain' ),
    ) );

    register_block_style( 'core/button', array(
        'name'  => 'ghost',
        'label' => __( 'Ghost', 'textdomain' ),
    ) );

    // Small styles can bring their CSS along
    register_block_style( 'core/quote', array(
        'name'         => 'callout',
        'label'        => __( 'Callout', 'textdomain' ),
        'inline_style' => '.wp-block-quote.is-style-callout { background: #f0f4ff; border-left: 4px solid #0066cc; padding: 1.5rem 2rem; }',
    ) );
}

inline_style is printed in the editor and on the front end. For several styles, put the CSS into a file and pass the handle of an enqueued stylesheet as style_handle instead.

The CSS

.wp-block-image.is-style-framed img {
    border: 8px solid #fff;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.wp-block-button.is-style-ghost .wp-block-button__link {
    background: transparent;
    border: 2px solid currentColor;
    color: inherit;
}

The stylesheet must also be loaded in the editor (for example with add_theme_support( 'editor-styles' ) and add_editor_style(), or on enqueue_block_assets), otherwise editors choose a style and see no difference.

Removing core styles

unregister_block_style() in PHP removes only styles that were registered in PHP. Core styles come from JavaScript and are removed there, after the editor has loaded:

wp.domReady( function () {
    wp.blocks.unregisterBlockStyle( 'core/button', 'outline' );
    wp.blocks.unregisterBlockStyle( 'core/quote', 'large' );
} );
<?php
add_action( 'enqueue_block_editor_assets', function () {
    wp_enqueue_script(
        'ha-block-styles',
        get_template_directory_uri() . '/js/block-styles.js',
        array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
        filemtime( get_template_directory() . '/js/block-styles.js' ),
        true
    );
} );

Blocks that already use a removed style keep their class; only the choice disappears from the panel.

NOTE: A block can have only one style at a time. For options that should combine (a frame and a shadow, for example), styles are the wrong tool; a custom block attribute or, for simple cases, the “Additional CSS class” field in the block’s Advanced panel works better.