admin管理员组文章数量:1122846
I've started with MarkJ's answer - JavaScript included into the block editor.
I wanted to control more attributes for gallery than linkTo
— the number of columns, cropping, and image size.
wp.domReady( function() {
wp.blocks.registerBlockVariation(
'core/gallery', {
isDefault: true,
attributes: {
columns: 6,
imageCrop: false,
linkTo: 'media',
sizeSlug: 'thumbnail', // does NOT work in WP 5.9.3
}
}
);
});
This snippet does the job partially. In WP editor block settings panel it looks like OK.
Unfortunately, in fact the size of every image in the gallery remains large
. Still needs an intervention (change to another size, then back to thumbnail
).
Is this a sizeSlug
bug, or
is there a better way to achieve custom defaults for Gallery Block?
I've started with MarkJ's answer - JavaScript included into the block editor.
I wanted to control more attributes for gallery than linkTo
— the number of columns, cropping, and image size.
wp.domReady( function() {
wp.blocks.registerBlockVariation(
'core/gallery', {
isDefault: true,
attributes: {
columns: 6,
imageCrop: false,
linkTo: 'media',
sizeSlug: 'thumbnail', // does NOT work in WP 5.9.3
}
}
);
});
This snippet does the job partially. In WP editor block settings panel it looks like OK.
Unfortunately, in fact the size of every image in the gallery remains large
. Still needs an intervention (change to another size, then back to thumbnail
).
Is this a sizeSlug
bug, or
is there a better way to achieve custom defaults for Gallery Block?
2 Answers
Reset to default 0Your understanding of what the default is, is incorrect. "isDefault
for new blocks" would be a more accurate interpretation, those existing blocks are not this variant.
Think of it this way, if I'm building a row of houses then the plans/blueprints are the default. Changing the plans does not magically transform all the houses that have already been built.
You've told WP when a gallery is created, these are the initial attributes to use when setting this variant. So new gallery blocks would have these attributes, assuming this variant.
Remember, variants are presets, if you create a gallery block that is not this variant, give it 6 columns, link to media, no crop, and a thumbnail, then it becomes this variant. Default values are initial values, not permanent dynamic values that change when you change the defaults.
Likewise, if you create a facebook embed, and modify its attributes to insert a twitter URL, then it becomes a twitter embed block, because they are all variants of core/embed
. The attributes determine which variant a block is, not the other way around.
I customized the default settings of the core/gallery
block in Wordpress 6.6 from within my theme's functions.php
:
function mytheme_block_type_metadata($settings, $metadata)
{
if ($settings['name'] == 'core/gallery') {
$settings['supports']['align'] = false;
$settings['attributes']['columns']['maximum'] = 1;
$settings['attributes']['linkTo']['default'] = 'media';
$settings['attributes']['sizeSlug']['default'] = 'mythemegallery';
} else if ($settings['name'] == 'core/image') {
$settings['attributes']['sizeSlug']['default'] = 'mythemegallery';
}
return $settings;
}
add_filter('block_type_metadata_settings', 'mytheme_block_type_metadata', 10, 2);
本文标签: customizationCustom default settings for WP native Gallery Block
版权声明:本文标题:customization - Custom default settings for WP native Gallery Block 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297598a1930045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论