Better Defaults for New Blocks
Changes the starting settings of blocks editors add: images link to nothing and open large, galleries crop evenly, columns stay side by side on phones, and new tables have header rows.
Opens WPIDE on
Code
add_action( 'enqueue_block_editor_assets', function () {
wp_register_script( 'wpide-block-defaults', false, array( 'wp-hooks', 'wp-blocks' ), '1.0', false );
wp_enqueue_script( 'wpide-block-defaults' );
wp_add_inline_script( 'wpide-block-defaults', <<<'JS'
( function ( wp ) {
// Block name => attribute => new default value.
var defaults = {
'core/image': { sizeSlug: 'large', linkDestination: 'none' },
'core/gallery': { imageCrop: true, linkTo: 'none' },
'core/columns': { isStackedOnMobile: true },
'core/table': { hasFixedLayout: true }
};
wp.hooks.addFilter( 'blocks.registerBlockType', 'wpide/block-defaults', function ( settings, name ) {
if ( ! defaults[ name ] || ! settings.attributes ) {
return settings;
}
var attributes = Object.assign( {}, settings.attributes );
Object.keys( defaults[ name ] ).forEach( function ( key ) {
if ( attributes[ key ] ) {
attributes[ key ] = Object.assign( {}, attributes[ key ], { default: defaults[ name ][ key ] } );
}
} );
return Object.assign( {}, settings, { attributes: attributes } );
} );
} )( window.wp );
JS
);
} );
Notes
Only affects newly inserted blocks. Delete any line in the defaults object you do not want.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.