admin管理员组文章数量:1122832
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?
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
Reset to default 1As 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
EDIT:
since WP 6.5.0 there is a filter get_block_type_variations
for adding/changing block variations, including extending variations on core blocks.
See: https://developer.wordpress.org/news/2024/03/14/how-to-register-block-variations-with-php/
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 );
本文标签: block variations registration in PHP
版权声明:本文标题:block variations registration in PHP 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306308a1932913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论