Custom Block Margin

Last modified: June 2, 2025

link Extension name

t2/custom-block-margin

link Setting version

You need to set a version in t2.json. This is used to determine which version
of the extension to use.

If you don't set a version, the extension will trigger a warning and fallback to
version 1. Latest (and recommended) version is 3.

{
	"t2/custom-block-margin": {
		"version": 3
	}
}

link v3

This version will override Block Spacing API for all containers with inner blocks in flow layout, including Post Content/root blocks, Group, Column, Media and Text, Cover, T2 Accordion, T2 Factbox, T2 Infobox, T2 Simple Media and Text, T2 Tabs etc. and enforce the same block spacing rules everywhere. (All current containers). Out of the box this extension uses the default spacing scale in WordPress.

link Extending

If you are using a different spacing scale for your project, or want to change any other part of the setup, you can use the built in filters:

t2/custom_block_margin/v3/gaps
t2/custom_block_margin/v3/default_gap
t2/custom_block_margin/v3/last_gap
t2/custom_block_margin/v3/last_selectors
t2/custom_block_margin/v3/containers

link Setting new spacing sizes

To change spacing sizes for a project first change the spacing sizes in theme.json (settings.spacing.spacingSizes). We recommend using the default spacing scale (20-80) for consistency, using 50 as medium reference point for gap between two paragraph blocks. If you need to add additional spacing sizes, add to the top (90) or bottom (10).

link Example: Changing spacing scale to only normal and small in theme.json.

{
	"settings": {
	"spacing": {
		"defaultSpacingSizes": false,
		"spacingSizes": [
		{
			"slug": "small",
			"name": "Small",
			"size": "1rem",
		},
		{
			"slug": "normal",
			"name": "Normal",
			"size": "2rem",
		},
		]
	}
	}
}

Then updated Custom Block Margin setup correspondingly with the same gaps and update the default and last gaps.

\add_filter( 't2/custom_block_margin/v3/gaps', function ( array $gaps ): array {
  return [
    'none' => [
      ...$gaps['none'],
    ],
    'small' => [
      ...$gaps['50'],
    ],
    'normal' => [],
  ];
} );

\add_filter( 't2/custom_block_margin/v3/default_gap', fn() => 'normal' );
\add_filter( 't2/custom_block_margin/v3/last_gap', fn() => 'normal' );

link For reference, this is the current default spacing scale in WordPress:

Key Value Alias
–wp–preset–spacing–20 0.44rem 2X-Small
–wp–preset–spacing–30 0.67rem X-Small
–wp–preset–spacing–40 1rem Small
–wp–preset–spacing–50 1.5rem Medium
–wp–preset–spacing–60 2.25rem Large
–wp–preset–spacing–70 3.38rem X-Large
–wp–preset–spacing–80 5.06rem 2X-Large

link Add/remove block containers:

Use the t2/custom_block_margin/v3/containers filter to add or remove supported containers. (All current containers)

\add_filter( 't2/custom_block_margin/v3/containers', function ( array $containers ): array {
  return [
    ...$containers,
    '.some-additional-container',
  ];
} );

link Legacy versions

link v2

Changing the margins can be done trough theme.json in your theme with the following structure:

{
	"settings": {
		"custom": {
			"t2/custom-block-margin": {
				"spacing": {
					"default": "3rem",
					"last": "5rem",
					"xSmall": "0.5rem",
					"small": "1rem",
					"medium": "3rem",
					"large": "5rem",
					"xLarge": "10rem"
				}
			}
		}
	}
	...
}

link v1

Add basic style margin between blocks on the site. The config allows you to set
3 different margins.

{
	"settings": {
		"custom": {
			"t2/custom-block-margin": {
				"spacing": {
					"normal": "3rem",
					"small": "1rem",
					"last": "5rem"
				}
			}
		}
	}
	...
}

link Hooks

link extension-library/src/custom-block-margin/v1.php

Filters root selector for custom block margin.

apply_filters( 't2/extension/custom_block_margin/root_selector', string $root )
Parameter Type Description
$root string Root selector. Changes based on frontend or backend context.

Filters base block margin selectors

apply_filters( 't2/extension/custom_block_margin/base_selectors', array $base_margin_selectors, string $root )
Parameter Type Description
$base_margin_selectors array Base block margin selectors.
$root string Root selector. Changes based on frontend or backend context.

Filters normal block margin selectors

apply_filters( 't2/extension/custom_block_margin/normal_selectors', array $normal_margin_selectors, string $root )
Parameter Type Description
$normal_margin_selectors array Normal block margin selectors.
$root string Root selector. Changes based on frontend or backend context.

Filters small block margin selectors

apply_filters( 't2/extension/custom_block_margin/small_selectors', array $small_margin_selectors, string $root )
Parameter Type Description
$small_margin_selectors array Small block margin selectors.
$root string Root selector. Changes based on frontend or backend context.

Filters zero top margin selectors.

apply_filters( 't2/extension/custom_block_margin/zero_top_selectors', array $zero_margin_top_selectors, string $root )
Parameter Type Description
$zero_margin_top_selectors array Zero top block margin selectors.
$root string Root selector. Changes based on frontend or backend context.

Filters last block bottom margin selectors

apply_filters( 't2/extension/custom_block_margin/last_selectors', array $selectors, string $root )
Parameter Type Description
$selectors array Last block bottom margin selectors.
$root string Root selector. Changes based on frontend or backend context.

link extension-library/src/custom-block-margin/v2.php

apply_filters( 't2/custom_block_margin/root_selector', $root_selector, $is_editor )
Parameter Type Description
$root_selector ``
$is_editor ``
apply_filters( 't2/custom_block_margin/config', $config, $root_selector, $is_editor, $wide_viewport_size )
Parameter Type Description
$config ``
$root_selector ``
$is_editor ``
$wide_viewport_size ``
apply_filters( 't2/custom_block_margin/{$gap_key}/size', $gap_config['size'] ?? '' )
Parameter Type Description
$gap_config['size'] ?? '' ``
apply_filters( 't2/custom_block_margin/{$gap_key}/selectors', $gap_config['selectors'] ?? [] )
Parameter Type Description
$gap_config['selectors'] ?? [] ``
apply_filters( 't2/custom_block_margin/last/selectors', [ "{$root_selector} > :last-child:not(.alignfull)" ], $root_selector )
Parameter Type Description
[ "{$root_selector} > :last-child:not(.alignfull)" ] ``
$root_selector ``
apply_filters( 't2/custom_block_margin/last/size_key', 'x-large' )
Parameter Type Description
'x-large' ``
apply_filters( 't2/custom_block_margin/default/size_key', 'large' )
Parameter Type Description
'large' ``

link extension-library/src/custom-block-margin/v3.php

Custom Block Margin v3 containers.
This filter allows you to change the containers for which the block margin is applied.

apply_filters( 't2/custom_block_margin/v3/containers', array $containers )
Parameter Type Description
$containers array The default containers.

Custom Block Margin v3 gaps.
This filter allows you to change the gaps for which the block margin is applied.

apply_filters( 't2/custom_block_margin/v3/gaps', array $gaps )
Parameter Type Description
$gaps array The default gaps.

Custom Block Margin v3 default gap.
This filter allows you to change the default gap for which the block margin is applied.

apply_filters( 't2/custom_block_margin/v3/default_gap', string $default_gap )
Parameter Type Description
$default_gap string The default gap

Custom Block Margin v3 gaps selectors.
This filter allows you to change the selectors for which the block margin is applied.

apply_filters( 't2/custom_block_margin/v3/gaps/{$key}/selectors', array $selectors )
Parameter Type Description
$selectors array The default selectors for the gap.

Custom Block Margin v3 last gap.
This filter allows you to change the last gap for which the block margin is applied.

apply_filters( 't2/custom_block_margin/v3/last_gap', string $last_gap )
Parameter Type Description
$last_gap string The last gap.

Custom Block Margin v3 last selectors.
This filter allows you to change the last selectors for which the block margin is applied.

apply_filters( 't2/custom_block_margin/v3/last_selectors', array $last_selectors, $root )
Parameter Type Description
$last_selectors array The default last selectors.
$root ``