Caution
This was built using AI, so make sure to read our DISCLAIMER file before using the plugin. Also, this is none-official plugin and has nothing to do with the WP AI Core team.
An AI Provider for OpenRouter for the PHP AI Client SDK. Works as both a Composer package and a WordPress plugin.
This package is based on the WordPress AI Provider for OpenAI package and adapts its provider implementation for OpenRouter.
- PHP 7.4 or higher
- When using with WordPress, targets WordPress 6.9+
- Requires the wordpress/php-ai-client SDK/plugin
First, add the GitHub repository to your composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/zaherg/ai-provider-for-openrouter"
}
]
}Then install the package:
composer require zaherg/ai-provider-for-openrouter:^0.1.0- Download the plugin files
- Upload to
/wp-content/plugins/ai-provider-for-openrouter/ - Ensure the PHP AI Client plugin is installed and activated
- Activate the plugin through the WordPress admin
The provider automatically registers itself with the PHP AI Client on the init hook. Simply ensure both plugins are active and configure your API key in wp-config.php:
define('OPENROUTER_API_KEY', 'your-api-key');
define('OPENROUTER_BASE_URL', 'https://openrouter.ai/api/v1'); // Optional override
// Use the provider
$result = AiClient::prompt('Hello, world!')
->usingProvider('openrouter')
->generateTextResult();use WordPress\AiClient\AiClient;
use Zaherg\OpenRouterAiProvider\Provider\OpenRouterProvider;
// Register the provider
$registry = AiClient::defaultRegistry();
$registry->registerProvider(OpenRouterProvider::class);
// Configure constants before using the provider (e.g. in wp-config.php for WordPress)
define('OPENROUTER_API_KEY', 'your-api-key');
define('OPENROUTER_BASE_URL', 'https://openrouter.ai/api/v1'); // Optional override
// Generate text
$result = AiClient::prompt('Explain quantum computing')
->usingProvider('openrouter')
->generateTextResult();
echo $result->toText();Available models are dynamically discovered from the OpenRouter /models endpoint.
This package currently registers text-output models and uses the OpenRouter OpenAI-compatible /responses endpoint for text generation (including function/tool calls where supported by the selected model).
Image generation and text-to-speech are intentionally not implemented in this package yet because OpenRouter's image workflows differ from the OpenAI /images/generations endpoint used by the upstream OpenAI provider.
Configure the provider via PHP constants (typically in wp-config.php):
define('OPENROUTER_API_KEY', 'your-api-key');
define('OPENROUTER_BASE_URL', 'https://openrouter.ai/api/v1'); // Optional overrideOpenRouter also recommends optional attribution headers such as HTTP-Referer and X-OpenRouter-Title. If you need those, set them through your HTTP transport/request options in the PHP AI Client stack.
GPL-2.0-or-later