Hooks

Docs Hooks Free · PHP hooks

modal_kit_meta_schema

filter

Free

PHP

Extend the JSON schema of a modal’s triggers, rules, overrides or on/off meta, for example to allow a new trigger type or store your own settings.

Each modal keeps its settings in four post meta values: enabled, triggers, rules and overrides, stored as _modal_kit_{key}. Their JSON schemas live in schema/modal-meta.json, and this filter receives one at a time, with its key.

The schema does three jobs: it is registered with register_post_meta() on init, so the REST API and the editor validate against it; saved values are validated and sanitised against it; and its default fills in missing keys when the plugin reads the meta. Pro adds its WooCommerce triggers, audience rules and teaser settings this way.

Parameters

Parameter

Description

$schema

array

The schema for one key: type, default and, for objects and arrays, properties or items. Objects set additionalProperties to false.

$key

string

enabled, triggers, rules or overrides.

Returns

array The schema, cast to an array. Keep to the JSON Schema subset the WordPress REST API understands.

Examples

Allow a new trigger type

The free runtime skips trigger types it doesn’t know, so your script opens the modal: dispatch modal-kit:request-open on its dialog. Add the type to the Triggers panel with the modalKit.triggerTypes editor filter.

PHP

add_filter( 'modal_kit_meta_schema', function ( $schema, $key ) {
  if ( 'triggers' !== $key ) {
    return $schema;
  }

  $types = (array) $schema['items']['properties']['type']['enum'];

  $schema['items']['properties']['type']['enum'] = array_values(
    array_unique( array_merge( $types, array( 'my-form-sent' ) ) )
  );
  $schema['items']['properties']['formId'] = array(
    'description' => 'Form sent: the HTML id of the form.',
    'type'        => 'string',
    'maxLength'   => 200,
  );

  return $schema;
}, 10, 2 );

Store your own settings per modal

Read them back with \ModalKit\Meta::get( $post_id, 'overrides' ), which fills in defaults.

PHP

add_filter( 'modal_kit_meta_schema', function ( $schema, $key ) {
  if ( 'overrides' !== $key ) {
    return $schema;
  }

  $schema['properties']['myCampaign'] = array(
    'type'                 => 'object',
    'additionalProperties' => false,
    'properties'           => array(
      'name'    => array(
        'type'      => 'string',
        'maxLength' => 60,
      ),
      'channel' => array(
        'type' => 'string',
        'enum' => array( 'email', 'social', 'search' ),
      ),
    ),
  );

  return $schema;
}, 10, 2 );

Good to know

Add the filter before init priority 10, where the meta is registered with the schema as it is then.

A value that fails validation isn’t trimmed: the whole meta value is replaced by the schema’s default. Declare every key you store.

To have a new key filled in when a saved value lacks it, add it to the schema’s default as well (for rules; overrides defaults to an empty object).

For rules, the plugin sets default.frequency.days from the site’s default wait after your filter runs.

The filter runs every time the plugin reads a schema, including on each meta read, so keep it cheap and return the same result every time.

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

Need a hook that isn’t here?