Hooks

Docs Hooks Free · PHP hooks

modal_kit_targets_request

filter

Free

PHP

Decide on the server whether a modal’s page targeting matches the page being built, for conditions Show on can’t express.

While a page is built, Modal Kit checks each enabled, published modal’s Show on setting (in Display rules) against the request and passes the answer here. Pro’s WooCommerce page rules (store, shop, category, product and out of stock) use this filter.

A modal that matches is printed with its automatic triggers. One that doesn’t is left out, unless the page links to it: then it is printed so the link works, but its automatic triggers don’t run.

Parameters

Parameter

Description

$targeted

bool

Whether the modal’s Show on setting matches this request.

$post

WP_Post

The modal.

Returns

bool Whether the modal targets this page, cast to a boolean.

Examples

Show a modal only around one category

On posts in the News category and on its archive, as long as Show on matches too.

PHP

add_filter( 'modal_kit_targets_request', function ( $targeted, $post ) {
  if ( 'news-signup' !== $post->post_name ) {
    return $targeted;
  }

  $in_news = is_category( 'news' )
    || ( is_single() && has_category( 'news', get_queried_object_id() ) );

  return $targeted && $in_news;
}, 10, 2 );

Let editors turn modals off on a page

A page with the my_no_modals custom field set gets no modals, except those it links to.

PHP

add_filter( 'modal_kit_targets_request', function ( $targeted ) {
  if ( is_singular() ) {
    $off = get_post_meta( get_queried_object_id(), 'my_no_modals', true );

    if ( $off ) {
      return false;
    }
  }

  return $targeted;
} );

Good to know

Only enabled, published modals reach the filter, and not those hidden from the current visitor (logged in, or an excluded role). The Display settings preview and the video lightbox skip it.

Decide from the request (the URL, the queried object), not from the visitor. Page caches and static exports keep one answer for everyone; time, device and visitor rules belong in the browser.

It can run twice for the same modal in one request, before the head and again in the footer, so return the same answer both times.

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

Need a hook that isn’t here?