Support

Account

Home Forums General Issues Block.json default attributes Reply To: Block.json default attributes

  • Yes, assigning defaults via block.json under attributes, as the WordPress documentation describes, is not reliable at all.

    I really wish ACF would do more to explain what is supported and what isn’t.

    I have resorted to using block variations to assign default attributes to my ACF blocks, because specifying them in block.json is useless:

    wp.domReady(function () {
    
    // USE BLOCK VARIATIONS TO ASSIGN DEFAULTS TO ACF BLOCKS
    
                wp.blocks.registerBlockVariation(
                    'acf/map', [{
                        isDefault: true,
                        attributes: {
                            style: {
                                dimensions: {
    								minHeight: '450px',
                                },
                            },
                        }
                    }]
                );
    });

    I place this in a .js file, then add this to functions:



    add_action('enqueue_block_editor_assets', 'block_modifications');
    function block_modifications()
    {
        wp_enqueue_script('block-modifications', get_template_directory_uri() . '/assets/js/block-modifications.js', array(
            'wp-blocks',
            'wp-dom-ready',
            'wp-edit-post'
        ), filemtime(get_template_directory() . '/assets/js/block-modifications.js'), true);
    }

    There is probably a less hacky way of doing this. It would be so much simpler if we could just set the defaults inside the block’s json file in the way the official WP docs describe.