Hooks

Docs Hooks Editor filters · JavaScript

modalKit.settingsTabs

filter

Free

JavaScript

Add, reorder or replace tabs on Modal Kit → Settings. Each tab is a React component, saved with Save Changes unless it opts out.

Modal Kit → Settings is one React app, Free’s modal-kit-settings script. It asks for its tabs through this filter on every render, shows them as wp-admin tabs, and picks the open one from the URL hash, so a tab named agency opens at #agency.

Free’s tabs are General, Display, Accessibility, Styles, Tools and Pro. Modal Kit Pro adds Results, A/B Tests and Integrations before Tools, and replaces Pro with License.

Parameters

Parameter

Description

tabs

Object[]

Tabs in order. Each has name (also the URL hash), title, Component and, optionally, saves.

Returns

Object[] Tabs. Component receives { notify }: call notify( status, message ) to show a notice above the tab. With saves: false the tab has no Save Changes button; otherwise the button saves every pending edit to the site settings (the root/site entity in @wordpress/core-data).

Examples

Add an Agency tab

The option is registered with show_in_rest (next example), so Save Changes saves it with the rest of the screen.

JavaScript

import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';
import { TextControl } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';

function AgencyTab() {
  const [ note, setNote ] = useEntityProp(
    'root',
    'site',
    'my_modal_brand_note'
  );

  return (
    <>
      <h2 className="title">{ __( 'Agency', 'my-plugin' ) }</h2>
      <TextControl
        __next40pxDefaultSize
        __nextHasNoMarginBottom
        label={ __( 'Brand note', 'my-plugin' ) }
        value={ note ?? '' }
        onChange={ setNote }
      />
    </>
  );
}

addFilter( 'modalKit.settingsTabs', 'my-plugin/agency', ( tabs ) => {
  const tab = {
    name: 'agency',
    title: __( 'Agency', 'my-plugin' ),
    Component: AgencyTab,
  };
  const index = tabs.findIndex( ( item ) => item.name === 'tools' );

  return index === -1
    ? [ ...tabs, tab ]
    : [ ...tabs.slice( 0, index ), tab, ...tabs.slice( index ) ];
} );

Register the option and load the script on the Settings screen

The screen is a submenu of the mk_modal post type, so its hook suffix is mk_modal_page_modal-kit-settings. Depending on modal-kit-settings loads your script after Free’s and only where Free’s loads.

PHP

add_action( 'init', function () {
  register_setting(
    'my_plugin',
    'my_modal_brand_note',
    array(
      'type'              => 'string',
      'default'           => '',
      'sanitize_callback' => 'sanitize_text_field',
      'show_in_rest'      => true,
    )
  );
} );

add_action( 'admin_enqueue_scripts', function ( $hook_suffix ) {
  if ( 'mk_modal_page_modal-kit-settings' !== $hook_suffix ) {
    return;
  }

  $asset = require plugin_dir_path( __FILE__ ) . 'build/settings.asset.php';

  wp_enqueue_script(
    'my-plugin-modal-settings',
    plugins_url( 'build/settings.js', __FILE__ ),
    array_merge( $asset['dependencies'], array( 'modal-kit-settings' ) ),
    $asset['version'],
    true
  );
  wp_set_script_translations( 'my-plugin-modal-settings', 'my-plugin' );
} );

Good to know

Save Changes goes through /wp/v2/settings, so a tab’s options must be registered with register_setting() and show_in_rest. The screen needs manage_options.

A hash that matches no tab opens the first one. Insert next to a tab you look up by name, and append when it isn’t there, so your tab still shows if another plugin removed it.

Free’s own form components aren’t exported. For the same look, use the wp-admin markup: <h2 class="title"> and <table class="form-table">.

Applied in modal-kit/src/admin/settings/index.js.

Need a hook that isn’t here?