Skip to main content

benecaster_ai_search_providers

Filter Premium ai-search

Adds your own search provider to the AI Search add-on’s Provider list on Settings → AI Search. $providers holds the four built-ins in dropdown order, openai (OpenAI), gemini (Google Gemini), cohere (Cohere) and anthropic (Anthropic), each as slug => [ 'label' => string, 'factory' => callable, 'models' => array ]. Add an entry and the podcaster can pick your provider; the add-on calls your factory with the stored key whenever it needs the provider.

You can add providers, never replace or remove a built-in: an entry that changes or drops one of the four is ignored, and the built-in stays. An entry is also ignored when its slug is not 1–32 characters of a-z, 0-9, _ or -, or it has an empty label or a factory that cannot be called. A factory that throws, or returns anything but a provider whose slug() is the entry’s slug, makes the provider fail with “The {label} provider could not be loaded.” Your exception’s own message is never shown, since it could hold the key.

Parameters

Name Type Default Description
$providers array — `[ slug => [ 'label' => string, 'factory' => callable( string $api_key, array $settings ): AISearchProviderInterface, 'models' => [ model => [ 'label' => string, 'description' => string ] ] ] ]`. `$settings['model']` is the model the podcaster chose, or `''` for your default. `models` is optional, your default first; leave it out and the screen offers no model choice.

Returns: array

Example

use BenecasterAiSearch\Provider\AISearchProviderInterface;

add_filter( 'benecaster_ai_search_providers', function ( array $providers ): array {
    $providers['localembed'] = [
        'label'   => __( 'Local Embed', 'my-plugin' ),
        'factory' => fn( string $api_key, array $settings ): AISearchProviderInterface => new My_Local_Embed_Provider( $api_key ),
    ];
    return $providers;
} );

Notes

The list is built once, on first use, so add your callback on or before init. Build the object in the factory and nothing more: never call your API from it. The provider class implements AISearchProviderInterface, whose supports_indexing() decides whether your provider builds an index of embeddings or ranks each search itself. Walkthrough: Add your own search provider to AI Search.

Need this built rather than just documented? See our services →