Hooks

Docs Hooks Pro · PHP hooks

modal_kit_pro_providers

filter

Pro

PHP

Add your own form destination to the Form block’s Send to list, posted from the browser or relayed through your site.

Builds the list of form destinations: the email services, the webhook and the WordPress plugins Pro sends to. Each item is an object that extends ModalKit\Pro\Providers\Provider. Add one and it appears under Send to in the Form block and on Modal Kit → Settings → Integrations.

A provider does one of two things. It gives the browser a public address to post to (transport post), or it returns transport relay with no address, so the browser hands the entry to your site and the site calls the provider’s send(). public_config() is printed in the page, so secrets only ever belong in send().

The list is built the first time Pro needs it in a request, then kept for the rest of that request.

Parameters

Parameter

Description

$list

Provider[]

The built-in providers, in the order the settings list them.

Returns

Provider[] The providers. Anything that isn’t a Provider is dropped, and a later provider with the same id() replaces an earlier one.

Examples

A CRM, sent from your site

The browser sends the entry to your site, which posts it to the CRM’s API with a key entered on Settings → Integrations. The key never reaches the page.

PHP

add_filter( 'modal_kit_pro_providers', function ( $providers ) {
  $providers[] = new class() extends \ModalKit\Pro\Providers\Provider {
    public function id() {
      return 'acme-crm';
    }

    public function label() {
      return __( 'Acme CRM', 'my-plugin' );
    }

    // Asks for an API key on Settings → Integrations.
    public function needs_key() {
      return true;
    }

    // Sent from the site, so it needs live WordPress.
    public function needs_live_site() {
      return true;
    }

    // What the page may see: no address, so the
    // browser hands the entry to your site.
    public function public_config( $form ) {
      return array(
        'url'       => '',
        'transport' => 'relay',
        'email'     => 'email',
      );
    }

    // $fields are keyed by each Field block's name.
    public function send( $form, $fields, $tags ) {
      $result = $this->api_json(
        'POST',
        'https://api.acme.example/v1/contacts',
        array( 'Authorization' => 'Bearer ' . $this->key() ),
        array(
          'email' => $fields['email'] ?? '',
          'name'  => $fields['name'] ?? '',
          'tags'  => $tags,
        )
      );

      if ( is_wp_error( $result ) ) {
        return $result;
      }

      if ( $result['code'] < 200 || $result['code'] >= 300 ) {
        return self::rejected( $result );
      }

      return true;
    }
  };

  return $providers;
} );

A public endpoint the browser posts to

For a service whose form endpoint accepts posts from other sites, like an embedded form’s action. A non-empty embedHelp adds a Form address box to the Form block; only addresses on the service’s own host are used. The endpoint must answer with CORS headers, or the browser treats the post as failed.

PHP

add_filter( 'modal_kit_pro_providers', function ( $providers ) {
  $providers[] = new class() extends \ModalKit\Pro\Providers\Provider {
    public function id() {
      return 'acme-forms';
    }

    public function label() {
      return __( 'Acme Forms', 'my-plugin' );
    }

    // emailKey must match 'email' in public_config().
    public function ui() {
      return array(
        'emailKey'  => 'contact[email]',
        'embedHelp' => __(
          'Paste the form address from Acme’s embed code.',
          'my-plugin'
        ),
      ) + parent::ui();
    }

    public function public_config( $form ) {
      $url  = esc_url_raw( (string) ( $form['actionUrl'] ?? '' ) );
      $safe = 'https' === wp_parse_url( $url, PHP_URL_SCHEME )
        && 'forms.acme.example' === wp_parse_url( $url, PHP_URL_HOST );

      return array(
        'url'       => $safe ? $url : '',
        'transport' => 'post',
        'email'     => 'contact[email]',
        'tagsField' => 'tags[]',
      );
    }
  };

  return $providers;
} );

Good to know

Must implement: id() (lower-case letters, digits and dashes; it’s stored in the block and used in REST routes), label() and public_config( $form ), which returns url, transport (post or relay) and email (the email field’s name), plus optional tagsField (one entry per tag) and extra (fixed fields the browser adds).

May implement: send( $form, $fields, $tags ) (by default it posts the fields form-encoded to public_config()’s url), group() (service, plugin or webhook: the Send to heading; any other value hides it), needs_key(), credential_fields(), verify_key() or verify_credentials() (checked before the key is saved), forget(), ui() (emailKey, listLabel, tags, note, embedHelp, keyHelp, keyNote, alias), lists() and tags() (each array( array( id, label ) ), or a WP_Error), needs_live_site(), connected(), detail() and settings_link(). Set $this->sent in send() to change “HTTP 200” in the Submissions log.

Helpers for send(): $this->key() and $this->credentials() read what was saved on the Integrations tab, api_json() and api_get() make requests, self::check_response() and self::rejected() turn answers into errors, person( $fields ) finds email and names, and cached() keeps lists() for ten minutes.

send() runs for relayed entries (POST modal-kit-pro/v1/submit) and for Send a test submission in the editor, which sends only the email field. It returns true or a WP_Error; an error is logged in Submissions and the visitor sees the form’s failure message. $fields are plain text, keyed by each Field block’s name, with the email under the provider’s email key and hidden fields included; ticked tag checkboxes arrive in $tags.

A post provider is still relayed when the form has a bot check or Also send to a webhook, and then its send() runs on the site. The default send() doesn’t add extra, so override it if your endpoint needs those fields.

In a must-use plugin, build the class inside the callback, as above. A named class at the top of the file would load before Pro and fail, because the base class doesn’t exist yet.

Applied in modal-kit-pro/includes/Integrations.php.

Need a hook that isn’t here?