WP OpenRouter Provider
Provides OpenRouter as an AI model provider for WordPress AI Client. Access 400+ AI models through a single API.
by Jonathan Bossenger · github.com/jonathanbossenger/wp-openrouter-provider · website
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/jonathanbossenger/wp-openrouter-provider/archive/refs/heads/trunk.zipReadme
WP OpenRouter Provider
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.
Features
- 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
Requirements
- WordPress 6.0 or higher
- PHP 8.0 or higher
- WordPress AI Client library
Installation
-
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.
Configuration
API Key
- Sign up for an account at openrouter.ai
- Get your API key from Settings > Keys
- Enter your API key in the plugin settings
Model Selection
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
Attribution Settings (Optional)
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.
Usage
Basic Usage
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 Selected Model
// 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)
Check if Provider is Registered
if (wp_openrouter_provider_is_registered()) {
// OpenRouter provider is available
}
Filters
wp_openrouter_provider_base_url
Customize the OpenRouter API base URL.
add_filter('wp_openrouter_provider_base_url', function($url) {
return 'https://openrouter.ai/api/v1'; // default
});
wp_openrouter_provider_models
Filter the models list before display.
add_filter('wp_openrouter_provider_models', function($models) {
// Remove specific models, add custom sorting, etc.
return $models;
});
wp_openrouter_provider_request_timeout
Customize the API request timeout (default: 30 seconds).
add_filter('wp_openrouter_provider_request_timeout', function($timeout) {
return 60; // 60 seconds
});
wp_openrouter_provider_cache_duration
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
});
Settings Options
| 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')) |
Popular Models
| 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.
Development
Running Tests
composer test
Code Standards
# Check code standards
composer phpcs
# Auto-fix code standards
composer phpcbf
License
GPL-2.0-or-later
Credits
- OpenRouter for providing the unified AI API
- WordPress AI Client for the provider framework