Hooks

Docs Hooks Free · PHP hooks

modal_kit_dialog_context

filter

Free

PHP

Change what the browser receives for a modal: its triggers, display rules, closing, focus and scroll settings.

Each printed modal is a <dialog> whose data-wp-context attribute holds a JSON object the front-end runtime reads: when to open (triggers and rules), how it closes and where focus goes. This filter changes that object just before the dialog’s markup is built, once the layout and panel are settled.

Pro’s age gate uses it to empty the triggers and turn off Esc and click outside, so the gate opens on page load and stays until it is answered. Pro’s teaser uses it too.

Parameters

Parameter

Description

$context

array

The runtime context: id, slug, layout, modal (bool: opens with showModal(), page behind inert), esc and outside (bool: Esc and a click outside close it), lockScroll (bool), focus (heading, first-field, first-button or close), autoClose (seconds, 0 when off), triggers (the triggers meta), targeted (bool: automatic triggers run), rules (frequency, devices, browsers, schedule, minPageViews), hidden, version and video.

$post

WP_Post

The modal. Its ID is 0 for the Display settings preview and the video lightbox.

$resolved

array

The Modal block’s resolved attributes: layout, position, size, mobileLayout, mobile, overlay, closeOptions, animation, modal, base and style.

Returns

array The context, cast to an array and printed as JSON in the dialog’s data-wp-context attribute.

Examples

Use a different trigger on blog posts

Triggers take the shape of the triggers meta. Here the newsletter modal waits until the reader is well into an article.

PHP

add_filter( 'modal_kit_dialog_context', function ( $context, $post ) {
  if ( 'newsletter' !== $post->post_name || ! is_singular( 'post' ) ) {
    return $context;
  }

  $context['triggers'] = array(
    array(
      'type'    => 'scroll',
      'percent' => 60,
    ),
  );

  return $context;
}, 10, 2 );

Close a modal only from its own buttons

Esc and a click outside stop working. The close button is part of the panel: turn it off in the modal’s Closing settings, or use a layout with closable set to false.

PHP

add_filter( 'modal_kit_dialog_context', function ( $context, $post ) {
  if ( 'terms-update' === $post->post_name ) {
    $context['esc']     = false;
    $context['outside'] = false;
    $context['focus']   = 'first-button';
  }

  return $context;
}, 10, 2 );

Good to know

Everything in the context is printed in the page’s HTML. Don’t put anything private in it.

Keys you add reach the browser too. A script of your own can read them from the modal-kit:configure event, fired on the dialog with the context as detail.config, before the triggers start.

The dialog’s classes come from the layout, not the context: changing layout here doesn’t restyle the modal. To change markup, use modal_kit_dialog_panel.

Decide from the page, not the visitor: page caches and static exports keep the context with the page.

Pro’s age gate (priority 10) and teaser (priority 20) use this filter. Hook in later to see their changes.

Applied in modal-kit/includes/Render.php.

Need a hook that isn’t here?