kimaai_register_addons

When it fires

Once per request, on plugins_loaded (priority 10), after KimaAI built its services and registered its own hooks. Right after the action KimaAI starts every registered add-on: it calls each add-on’s register() method with $main and $classes, in the order they were registered, and then fires kimaai_loaded. In a WP-CLI run, KimaAI also calls an add-on’s registerCommands(Main $main, ClassesSurface $classes) method on cli_init, when the add-on has one, so it can add its own wp commands.

An add-on is an object that implements KimaAI\Addons\AddonInterface, whose one method is register(Main $main, ClassesSurface $classes). Add the callback when your plugin’s main file loads, not in a plugins_loaded callback of your own, which may run after KimaAI’s. Nothing fires when KimaAI is not active, so your add-on stays off then.

Declare your add-on’s class where it runs only with KimaAI loaded: inside the callback, as the example does with an anonymous class, or in a file of its own that the callback requires. A class that implements the interface at the top of your plugin’s main file stops the site with a fatal error when KimaAI is inactive or loads after your plugin.

Parameters

NameTypeDescription
managerAddonManagerKimaAI’s add-on manager (KimaAI\Addons\AddonManager); pass your add-on to its register() method.
mainMainKimaAI’s main object (KimaAI\Main).
classesClassesSurfaceAccess to KimaAI’s services (KimaAI\Surfaces\ClassesSurface); get() returns the service of a class.

Example

use KimaAI\Addons\AddonInterface;
use KimaAI\Addons\AddonManager;
use KimaAI\Main;
use KimaAI\Surfaces\ClassesSurface;

add_action(
  'kimaai_register_addons',
  function (AddonManager $manager): void {
    $manager->register(
      new class() implements AddonInterface {
        public function register(Main $main, ClassesSurface $classes): void {
          add_filter('kimaai_chatbot_instructions', [$this, 'addHolidayNotice']);
        }

        public function addHolidayNotice(string $instructions): string {
          return $instructions . "\nThe shop is closed from 24 to 26 December. Orders placed then ship on 27 December.";
        }
      }
    );
  }
);

The add-on adds a line to the chatbot’s instructions. register() is the place for an add-on’s hooks; it runs on every request, so keep it to adding hooks and leave work to the hooks themselves.

The services in $classes are KimaAI’s own classes. They are not a public interface and can change between releases, so prefer KimaAI’s hooks where one fits.

The guide “Build an add-on” (docs/guides/add-on.md) builds a whole add-on: its plugin file, its class in a file of its own and a WP-CLI command.

  • kimaai_loaded: fires after the add-ons started, for code without an add-on class.
  • kimaai_settings_schema_files: adds the add-on’s settings page.
  • kimaai_chatbot_tools_registry: a tool the add-on offers to the chatbot.

What are your feelings