kimaai_trigger_button_click

When it fires

In the browser, on the KimaAI settings page, when someone clicks a button drawn by the TriggerButton field component. KimaAI’s own settings use no such button; it is there for add-ons, which put one on their own settings page through kimaai_settings_schema_files. The button does nothing itself: it saves no setting and sends no request, it only fires this action.

A button is a field of the add-on’s schema file with the component TriggerButton. Its props give the label (text) and the look (variant: primary, secondary, tertiary or link). 'type' => null keeps it out of the stored settings, as KimaAI does for its own entries that store nothing:

'myAddonClearCache' => [
  'type' => null,
  'component' => 'TriggerButton',
  'props' => [
    'text' => __('Clear cache', 'my-addon'),
    'variant' => 'secondary',
  ],
],

Every TriggerButton fires the same action, so a callback tells them apart by button.key, the field’s key (myAddonClearCache above). Callbacks written for earlier versions, which got only text and variant, keep working. The key is set on a settings page; a TriggerButton inside the entry form of a list gets none. Pick keys that no other add-on is likely to use, with your add-on’s name in them.

The action runs in the admin’s JavaScript, so the script that adds the callback is loaded on the KimaAI settings page, with wp-hooks as a dependency (and wp-api-fetch for the example below):

add_action(
  'admin_enqueue_scripts',
  function (string $hook_suffix): void {
    if ($hook_suffix !== 'toplevel_page_kimaai') {
      return;
    }

    wp_enqueue_script('my-addon-settings', plugins_url('settings.js', __FILE__), ['wp-hooks', 'wp-api-fetch'], '1.0.0', true);
  }
);

Parameters

NameTypeDescription
eventEventThe click as React passes it, with target, preventDefault() and nativeEvent, the browser’s event.
buttonObjectThe button, with text (its label), variant and key, the key of its field in the settings schema.

Example

wp.hooks.addAction('kimaai_trigger_button_click', 'my-addon/clear-cache', function (event, button) {
  if (button.key !== 'myAddonClearCache') {
    return;
  }

  wp.apiFetch({path: '/my-addon/v1/clear-cache', method: 'POST'});
});

The callback answers only its own button, the field with the key myAddonClearCache, and calls a REST route of the add-on, which does the work on the server and checks the user’s capability there. wp.apiFetch sends the logged-in user’s nonce with the request.

  • kimaai_settings_schema_files: adds the add-on’s settings page that holds the button.
  • kimaai.admin.fieldComponents: draws a field with the add-on’s own component instead of a button.

What are your feelings