3

Currently, both register_block_style and register_block_type exist in PHP as alternative to the same functions in javascript registerBlockStyle and registerBlockType. However, it seems that register_block_variation does not exist. Is there possibility to register core block variation from PHP?

2 Answers 2

0

As of January 2023, it seems that this function remains unavailable in PHP. The only issue where I find any mention to it in Gutenberg repo is this one: https://github.com/WordPress/gutenberg/issues/24994

I've opened an issue requesting it, let's see if they can handle it :) https://github.com/WordPress/gutenberg/issues/47170

1

I found a workaround. Following snippet registers variant of post-excerpt block which will have only three lines of text. It does so by intercepting metadata from block.json and injecting new.

<?php
function extended_query_excerpt_block_args($args) {
    $args['render_callback'] = 'render_block_core_post_excerpt';
    $args['attributes']['variantType'] = [
        'type' => 'string'
    ];
    $args['variations'] = [
        [
            'name' => 'three-lines-excerpt',
            'title' => 'Three lines excerpt',
            'attributes' => [
                'variantType' => 'three-lines-excerpt'
            ],
        ]
    ];
    return $args;
}

function register_variation($args, $block_type) {
    switch ($block_type) {
        case 'core/post-excerpt':
            $args = extended_query_excerpt_block_args($args);
            break;
    }
    return $args;
}

add_filter( 'register_block_type_args', 'register_variation', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.