Hooks

Docs Hooks Free · PHP hooks

modal_kit_pattern_collections

filter

Free

PHP

Add template collections to the picker: named groups of starters listed under Collections. Pro adds Studio, Shop, Popups and more.

Filters the template collections. The free plugin has none; Modal Kit Pro adds Studio, Shop, SaaS & apps, Events, Nonprofit, Seasonal, Games and Popups. Each slug is registered as a block pattern category on init (priority 20), and the template picker lists collections under Collections, after its categories.

A starter belongs to a collection when the collection’s slug is one of its pattern categories. It can be in a category as well, and then shows in both.

Parameters

Parameter

Description

$collections

array<string, string>

Slug => label. Empty by default; Pro’s are added at priority 10.

Returns

array<string, string> Slug => label, cast to an array.

Examples

Add a collection and a starter in it

This starter is in the collection and in the Newsletter category.

PHP

add_filter( 'modal_kit_pattern_collections', function ( $collections ) {
  return array_merge(
    (array) $collections,
    array( 'my-plugin-agency' => __( 'Agency', 'my-plugin' ) )
  );
} );

add_action( 'init', function () {
  register_block_pattern(
    'my-plugin/client-newsletter',
    array(
      'title'      => __( 'Client newsletter', 'my-plugin' ),
      'categories' => array( 'my-plugin-agency', 'modal-kit-newsletter' ),
      'postTypes'  => array( 'mk_modal' ),
      'inserter'   => false,
      'content'    => '<!-- wp:modal-kit/modal -->'
        . '<!-- wp:heading --><h2 class="wp-block-heading">'
        . esc_html__( 'Get the monthly letter', 'my-plugin' )
        . '</h2><!-- /wp:heading -->'
        . '<!-- /wp:modal-kit/modal -->',
    )
  );
} );

Load pattern files the way Pro does

Pro keeps each template in a file that returns an array (slug, title, categories, content and optional settings) and reads it with ModalKit\Patterns::load(), which also sets the post type, keeps it out of the inserter and collects its settings for modal_kit_starter_settings.

PHP

add_filter( 'modal_kit_pattern_collections', function ( $collections ) {
  return array_merge(
    (array) $collections,
    array( 'my-plugin-agency' => __( 'Agency', 'my-plugin' ) )
  );
} );

add_action( 'init', function () {
  if ( ! class_exists( 'ModalKit\Patterns' ) ) {
    return;
  }

  $files = glob( __DIR__ . '/patterns/*.php' );

  foreach ( $files ? $files : array() as $file ) {
    $pattern = \ModalKit\Patterns::load( $file );

    if ( $pattern ) {
      register_block_pattern( $pattern['name'], $pattern['properties'] );
    }
  }
}, 21 );

Good to know

Every starter in a collection gets a PRO badge on its card in the picker.

Add the filter before init priority 20, when collections are registered as pattern categories.

The picker sorts collections A–Z and hides the ones with no starters.

Use array_merge() or add a key, as above, so Pro’s collections stay. To add type filters inside a collection, use modal_kit_pattern_collection_types.

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

Need a hook that isn’t here?