A WordPress plugin that provides OpenRouter as an AI model provider for the WordPress AI Client library. This enables access to 400+ AI models from various providers (Anthropic, OpenAI, Meta, Google, and more) through a single, unified API.
- 400+ AI Models: Access models from Anthropic (Claude), OpenAI (GPT-4), Meta (Llama), Google (Gemini), Mistral, and many more
- OpenAI-Compatible API: Uses OpenRouter's OpenAI-compatible endpoint for seamless integration
- Searchable Model Selection: Select2-powered dropdown with search, provider filtering, and free models toggle
- Model Information Display: View context length, pricing, and descriptions for each model
- Attribution Headers: Optional HTTP-Referer and X-Title headers for usage tracking
- Caching: 10-minute transient caching for model list to reduce API calls
- Extensible: Multiple filters for customizing behavior
- WordPress 6.0 or higher
- PHP 8.0 or higher
- WordPress AI Client library
-
Download or clone this repository into your
wp-content/plugins/directory:cd wp-content/plugins/ git clone https://github.com/jonathanbossenger/wp-openrouter-provider.git -
Install Composer dependencies:
cd wp-openrouter-provider composer install -
Activate the plugin in WordPress admin under Plugins.
-
Configure the plugin at Settings > OpenRouter AI.
- Sign up for an account at openrouter.ai
- Get your API key from Settings > Keys
- Enter your API key in the plugin settings
The settings page provides a searchable dropdown with all available models. You can:
- Search by model name
- Filter by provider (Anthropic, OpenAI, Meta, etc.)
- Show only free models
- View model details (context length, pricing) when selected
OpenRouter supports attribution headers to track usage from your site:
- HTTP-Referer: Your site URL
- X-Title: Your site name
These are enabled by default and auto-populated from your WordPress settings.
Once configured, the OpenRouter provider is automatically registered with the WordPress AI Client:
// Get the AI Client registry
$registry = \WordPress\AiClient\AiClient::defaultRegistry();
// Get a specific model
$model = $registry->getModel('openrouter', 'anthropic/claude-3.5-sonnet');
// Use the model for text generation
// (Refer to WordPress AI Client documentation for generation methods)// Get the model ID selected in settings
$model_id = wp_openrouter_provider_get_selected_model();
// Returns: 'anthropic/claude-3.5-sonnet' (or whatever is selected)if (wp_openrouter_provider_is_registered()) {
// OpenRouter provider is available
}Customize the OpenRouter API base URL.
add_filter('wp_openrouter_provider_base_url', function($url) {
return 'https://openrouter.ai/api/v1'; // default
});Filter the models list before display.
add_filter('wp_openrouter_provider_models', function($models) {
// Remove specific models, add custom sorting, etc.
return $models;
});Customize the API request timeout (default: 30 seconds).
add_filter('wp_openrouter_provider_request_timeout', function($timeout) {
return 60; // 60 seconds
});Customize how long the models list is cached (default: 10 minutes).
add_filter('wp_openrouter_provider_cache_duration', function($duration) {
return HOUR_IN_SECONDS; // Cache for 1 hour
});| Option Name | Type | Default | Description |
|---|---|---|---|
wp_openrouter_provider_api_key |
string | '' | OpenRouter API key |
wp_openrouter_provider_model |
string | '' | Selected model ID |
wp_openrouter_provider_enable_attribution |
bool | true | Enable attribution headers |
wp_openrouter_provider_site_url |
string | '' | Site URL for HTTP-Referer (defaults to get_site_url()) |
wp_openrouter_provider_site_name |
string | '' | Site name for X-Title (defaults to get_bloginfo('name')) |
| Model ID | Description |
|---|---|
anthropic/claude-3.5-sonnet |
Best balance of quality and speed |
openai/gpt-4-turbo |
Great for complex reasoning |
meta-llama/llama-3-70b |
Open-source alternative |
google/gemini-pro |
Good for general tasks |
mistralai/mistral-large |
Strong European alternative |
See the full list at openrouter.ai/models.
composer test# Check code standards
composer phpcs
# Auto-fix code standards
composer phpcbfGPL-2.0-or-later
- OpenRouter for providing the unified AI API
- WordPress AI Client for the provider framework