Kishan Jasani

Code, Creativity & Everything I Learn

Block Variation is Not (!==) Block Style

Block Styles v/s Block variations

The Crucial Difference Most Developers Miss

In the WordPress block ecosystem, confusion between block styles and block variations can lead to suboptimal implementations. Let’s clarify these concepts once and for all!

đź’ˇ Quick Summary: Block Styles change how blocks look. Block Variations change what blocks do.

Let’s break down the differences, explore examples, and discuss which approach works best for long-term solutions.

Block Style vs. Block Variation: What’s the Difference?

FeatureBlock StyleBlock Variation
PurposeChanges the visual appearance of an existing blockCreates a pre-configured variation of an existing block with different attributes
AffectsCSS stylesBlock settings and attributes
Applied UsingregisterBlockStyle()registerBlockVariation()
ExampleA button block with a “rounded” or “outlined” styleA button block preconfigured as a call-to-action (CTA) button
Best Use CaseWhen only the design needs to changeWhen multiple attributes need to be modified and predefined

Block Styles: When Appearance Matters

Block styles provide alternative visual options for existing blocks without changing their core functionality.

Code Example: Adding a Rounded Button Style

JavaScript

wp.domReady( () => {
    wp.blocks.registerBlockStyle( 'core/button', {
        name: 'rounded',
        label: 'Rounded',
    } );
} );

PHP

If you prefer to register the block style using PHP, you can do it like this:

function register_custom_block_styles() {
    register_block_style(
        'core/button',
        [
            'name'  => 'rounded',
            'label' => __('Rounded', 'text-domain'),
            'inline_style' => '.wp-block-button.is-style-rounded .wp-block-button__link { border-radius: 50px; }'
        ]
    );
}
add_action( 'init', 'register_custom_block_styles' );

What This Does:

  • Adds a “Rounded” option in the block’s Style selector
  • Applies the class is-style-rounded to the block
  • Styles can be defined in your CSS:
.wp-block-button.is-style-rounded .wp-block-button__link {
    border-radius: 50px;
}

Block Styles: Pros & Cons

âś… Pros

  • Lightweight implementation
  • Simple to maintain
  • Preserves block defaults

❌ Cons

  • Limited to visual changes
  • Can’t alter content structure
  • No control over default attributes

Block Variations: When Function Matters

Block variations create pre-configured versions of existing blocks with different attributes and potentially different inner blocks.

Code Example: Creating a CTA Button Variation

  • create a file called block-variations.js and add this code inside that
wp.domReady( () => {
    wp.blocks.registerBlockVariation( 'core/button', {
        name: 'cta-button',
        title: 'CTA Button',
        attributes: {
            backgroundColor: 'blue',
            textColor: 'white',
            className: 'cta-button',
        },
        innerBlocks: [
            [ 'core/paragraph', { content: 'Click here to learn more' } ]
        ]
    } );
} );

What this code does?

  • This registers a new variation of the button block called CTA Button.
  • The button is pre-configured with:
    • A blue background
    • White text color
    • A default text inside a paragraph block
  • This allows content creators to quickly add a standardized CTA button without manual configuration.

how to properly enqueue the script for registering block variations in WordPress:

<?php
/**
 * Enqueue script for registering block variations
 */
function enqueue_block_variation_script() {
    // Register the script.
    wp_register_script(
        'custom-block-variations',
        get_template_directory_uri() . '/js/block-variations.js',
        array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
        filemtime( get_template_directory() . '/js/block-variations.js' ),
        true
    );

    // Enqueue the script in the editor
    wp_enqueue_script( 'custom-block-variations' );
}
add_action( 'enqueue_block_editor_assets', 'enqueue_block_variation_script' );

Block Variations: Pros & Cons

âś… Pros

  • Highly customizable
  • Can pre-configure multiple attributes
  • Supports inner blocks and complex structures
  • Creates consistent components

❌ Cons

  • More complex implementation
  • Requires more maintenance

Removing Styles and Variations

If you need to remove a previously registered block style:

// Remove a block style
wp.domReady( () => {
    wp.blocks.unregisterBlockStyle( 'core/button', 'rounded' );
} );

// Remove a block variation
wp.domReady( () => {
    wp.blocks.unregisterBlockVariation( 'core/button', 'cta-button' );
} );

Best Approach for Long-Term Solutions

For long-term maintainability and flexibility:

  • If you need pre-configured content and structure, block variations are the way to go.
  • For maximum flexibility, use block variations. This approach allows you to define reusable layouts while giving users the ability to tweak designs.

By leveraging both approaches strategically, you can create a better user experience and maintain a scalable, future-proof block-based theme. 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *