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?

Share Improve this question asked Apr 11, 2022 at 8:51 RafiozooRafiozoo 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Your 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