Skip to main content

Offer AI models from the catalog in your add-on’s settings, with your shipped list as the fallback

Free Advanced

Build a model dropdown that picks up new models without a release of your add-on, but never offers one your version cannot call. benecaster_get_ai_model_catalog() returns the provider’s list, or [], which means “use your shipped list”.

Code

<?php
const MY_ADDON_SLUG    = 'benecaster-addon-my-ai-tool';
const MY_ADDON_VERSION = '1.4.0';

function my_addon_model_choices( string $provider, string $kind, array $shipped ): array {
    $choices = [];

    foreach ( benecaster_get_ai_model_catalog( $provider ) as $model ) {
        $needs = $model['min_addon_version'][ MY_ADDON_SLUG ] ?? null;
        if ( null === $needs || version_compare( MY_ADDON_VERSION, $needs, '<' ) ) {
            continue; // Not for this add-on, or needs a newer release of it.
        }
        if ( $model['kind'] !== $kind || $model['retired'] ) {
            continue;
        }
        // Prefer your own translated label when you ship this model too.
        $choices[ $model['id'] ] = $shipped[ $model['id'] ] ?? $model['label'];
    }

    return [] === $choices ? $shipped : $choices; // [] means "use your shipped list".
}

View on GitHub →

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