Custom Block Margin
Extension name
t2/custom-block-margin
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
}
}
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.
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
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).
normal
and small
in theme.json
.
Example: Changing spacing scale to only {
"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' );
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 |
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',
];
} );
Legacy versions
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"
}
}
}
}
...
}
v1
Add basic style margin between blocks on the site. The config allows you to set
3 different margins.
normal
: Default block margin.small
: Margin used on blocks that needs less space between them. Ex. two paragraphs after each other.last
: Last block in block context container (except full width blocks).
{
"settings": {
"custom": {
"t2/custom-block-margin": {
"spacing": {
"normal": "3rem",
"small": "1rem",
"last": "5rem"
}
}
}
}
...
}
Hooks
extension-library/src/custom-block-margin/v1.php
Filters root selector for custom block margin v1.
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 v1.
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 v1.
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 v1.
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 v1.
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 v1.
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. |
extension-library/src/custom-block-margin/v2.php
Filters root selector for custom block margin v2.
apply_filters( 't2/custom_block_margin/root_selector', string $root, bool $is_editor )
Parameter | Type | Description |
---|---|---|
$root |
string |
Root selector. Changes based on frontend or backend context. |
$is_editor |
bool |
Is editor conditional. |
Filters the Custom Block Margin config v2.
apply_filters( 't2/custom_block_margin/config', array $config, string $root_selector, bool $is_editor, int $wide_viewport_size )
Parameter | Type | Description |
---|---|---|
$config |
array |
The config. |
$root_selector |
string |
The root selector. |
$is_editor |
bool |
Is editor conditional. |
$wide_viewport_size |
int |
The wide viewport size. |
Filters the gap size for Custom Block Margin v2.
apply_filters( 't2/custom_block_margin/{$gap_key}/size', array|string $gap_size )
Parameter | Type | Description |
---|---|---|
$gap_size |
array|string |
The gap size as array with min/max values or single value. |
Filters the gap selectors for Custom Block Margin v2.
apply_filters( 't2/custom_block_margin/{$gap_key}/selectors', array $selectors )
Parameter | Type | Description |
---|---|---|
$selectors |
array |
The gap selectors. |
Filters last block bottom margin selectors v2.
apply_filters( 't2/custom_block_margin/last/selectors', array $selectors, string $root_selector )
Parameter | Type | Description |
---|---|---|
$selectors |
array |
Last block bottom margin selectors. |
$root_selector |
string |
Root selector. Changes based on frontend or backend context. |
Filters the last block bottom margin size key for Custom Block Margin v2.
apply_filters( 't2/custom_block_margin/last/size_key', string $last_gap_key )
Parameter | Type | Description |
---|---|---|
$last_gap_key |
string |
Last block bottom margin size key. Default x-large. |
Filters the default gap size key for Custom Block Margin v2.
apply_filters( 't2/custom_block_margin/default/size_key', string $default_gap_key )
Parameter | Type | Description |
---|---|---|
$default_gap_key |
string |
Default gap size key. Default large. |
extension-library/src/custom-block-margin/v3.php
Custom Block Margin containers v3.
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 gaps v3.
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 default gap v3.
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. Default 80. |
Custom Block Margin gaps selectors v3.
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 last gap v3.
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. Default 80. |
Custom Block Margin last selectors v3.
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, string $root )
Parameter | Type | Description |
---|---|---|
$last_selectors |
array |
The default last selectors. |
$root |
string |
The root selector. Changes based on frontend or backend context. |