Hooks
modal_kit_layouts
filter
Free
PHP
Add a layout for the Modal block, or change a built-in one. Pro adds its age gate this way.
Modal Kit’s layouts (Centered, Corner, Bar, Full-screen, Drawer, Split, Sheet, Inline and Video) come from schema/layouts.json, which the editor reads too. This filter receives them keyed by name each time the plugin reads the list: when a modal is rendered, when its block attributes are resolved, and for the Layout column of the modal list.
A layout sets overlay, modal, positions and mobile. An add-on layout can also set base, a built-in layout whose styles it extends (its class is added to the dialog too), closable (false removes the close button, Esc and click outside, whatever the modal’s Closing settings say) and label, shown in the modal list.
Parameters
Parameter
Description
$layouts
array
Layouts keyed by name. Each has overlay (bool: the layout has an overlay, which authors can turn off), modal (bool: opens with showModal(), so the page behind is inert), positions (string[]: the first is the default; empty for none) and mobile (string: the layout used on phones when Mobile is automatic).
Returns
array The layouts. The result is used only when it is an array that still contains centered; otherwise the built-in list is kept.
Examples
Add a layout that can’t be dismissed
It looks like Centered, goes full screen on phones, and has no close button, Esc or click outside: only a button with the Close modal action closes it.
add_filter( 'modal_kit_layouts', function ( $layouts ) {
$layouts['my-notice'] = array(
'label' => __( 'Notice', 'my-plugin' ),
'overlay' => true,
'modal' => true,
'positions' => array(),
'mobile' => 'full',
'base' => 'centered',
'closable' => false,
);
return $layouts;
} );
Add a layout with styles of its own
A bar with its own touches. The dialog carries modal-kit--my-ribbon and modal-kit--bar, so the Bar styles apply and yours go on top.
add_filter( 'modal_kit_layouts', function ( $layouts ) {
$layouts['my-ribbon'] = array(
'label' => __( 'Ribbon', 'my-plugin' ),
'overlay' => false,
'modal' => false,
'positions' => array( 'top', 'bottom' ),
'mobile' => 'bar-bottom',
'base' => 'bar',
);
return $layouts;
} );
// The Modal block's stylesheet loads on the site and in the editor
// canvas. It is registered on init, priority 10.
add_action( 'init', function () {
wp_add_inline_style(
'modal-kit-modal-style',
'.modal-kit--my-ribbon > .modal-kit__panel {'
. ' text-align: center; font-weight: 600; }'
);
}, 20 );
Good to know
This is the server side only. The editor reads its own list: add the same layout with the modalKit.layouts JavaScript filter, with its editor labels, or authors can’t choose it.
Every layout needs overlay, modal, positions and mobile. They are read without defaults: a layout missing one causes PHP warnings, and one without positions a fatal error on PHP 8.
The dialog gets modal-kit--{name}, modal-kit--{base} when base names a built-in layout, modal-kit--pos-{position} and modal-kit--m-{mobile}. Without base, the layout has no styles until you add them. When an author sets Mobile to the same as desktop, the mobile class is modal-kit--m-{name}, which only your CSS styles.
A modal saved with a layout that no longer exists, because its add-on was turned off, falls back to Centered.
The filter runs every time the list is read, several times per modal, so keep the callback cheap.
Applied in modal-kit/includes/Layouts.php.