Hooks

Docs Hooks Free · PHP hooks

modal_kit_dialog_panel

filter

Free

PHP

Change a modal’s rendered panel before the dialog is built: add markup, attributes or classes. Pro adds step progress here.

The panel is the Modal block’s rendered output: a div.modal-kit__panel holding the inner blocks, the close button and, when auto-close is on, its Pause button. This filter receives it before Modal Kit names the dialog after its first heading, prepares any video and wraps the panel in <dialog>.

Pro uses it for step progress, forms, product data, open animations, age checks and A/B tests. It runs while the page is built (before the head in block themes), so a callback can also enqueue a stylesheet or script module for the modals that need one.

Parameters

Parameter

Description

$panel

string

The panel’s HTML.

$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, as in modal_kit_dialog_context: layout, position, size, mobileLayout, mobile, overlay, closeOptions, animation, modal, base and style.

$attrs

array

The Modal block’s attributes as saved, after the core render_block_data filter.

Returns

string The panel’s HTML, cast to a string. It isn’t escaped again, so escape what you add.

Examples

Tag the panel for your analytics

PHP

add_filter(
  'modal_kit_dialog_panel',
  function ( $panel, $post, $resolved ) {
    if ( ! $post->ID ) {
      return $panel;
    }

    $processor = new WP_HTML_Tag_Processor( $panel );

    if ( ! $processor->next_tag( array( 'class_name' => 'modal-kit__panel' ) ) ) {
      return $panel;
    }

    $processor->set_attribute( 'data-my-campaign', $post->post_name );
    $processor->set_attribute( 'data-my-layout', $resolved['layout'] );

    return $processor->get_updated_html();
  },
  10,
  3
);

Add a line at the end of one modal

The panel ends with its own closing </div>, so the line goes in just before it.

PHP

add_filter( 'modal_kit_dialog_panel', function ( $panel, $post ) {
  if ( 'spring-sale' !== $post->post_name ) {
    return $panel;
  }

  $note = sprintf(
    '<p class="my-terms has-small-font-size">%s</p>',
    esc_html__( 'Offer ends Sunday. Terms apply.', 'my-plugin' )
  );
  $end  = strrpos( $panel, '</div>' );

  return false === $end
    ? $panel . $note
    : substr( $panel, 0, $end ) . $note . substr( $panel, $end );
}, 10, 2 );

Good to know

Change attributes with WP_HTML_Tag_Processor where you can: string replacement breaks easily when the blocks inside change.

A heading you add before the modal’s own becomes the dialog’s accessible name, because the first heading names it by default.

Interactivity API directives you add (data-wp-*) are processed with the rest of the dialog, in the modal-kit namespace unless you set your own data-wp-interactive.

Pro hooks in at priorities 5 to 40, so the panel you receive may already carry its markup.

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

Need a hook that isn’t here?