AI Plugin Development for WordPress
AI plugin for WordPress adds language models to WordPress ecosystem: content generation in editor block, AI chat widget for visitors, automatic SEO metadata, article summarization. Technically, PHP backend + JavaScript (React/Vanilla) frontend within WordPress Plugin API.
Plugin Architecture
<?php
/**
* Plugin Name: AI Assistant
* Version: 1.0.0
*/
if (!defined('ABSPATH')) exit;
class AIAssistant {
private string $api_key;
public function __construct() {
$this->api_key = get_option('ai_assistant_api_key', '');
add_action('rest_api_init', [$this, 'register_endpoints']);
add_action('init', [$this, 'register_block']);
add_action('admin_menu', [$this, 'add_settings_page']);
}
public function register_endpoints(): void {
register_rest_route('ai-assistant/v1', '/generate', [
'methods' => 'POST',
'callback' => [$this, 'generate_content'],
'permission_callback' => function() {
return current_user_can('edit_posts');
},
]);
register_rest_route('ai-assistant/v1', '/chat', [
'methods' => 'POST',
'callback' => [$this, 'chat_response'],
'permission_callback' => '__return_true',
]);
}
public function generate_content(\WP_REST_Request $request): \WP_REST_Response {
$prompt = sanitize_text_field($request->get_param('prompt'));
$type = sanitize_key($request->get_param('type'));
if (empty($this->api_key)) {
return new \WP_REST_Response(['error' => 'API key not configured'], 400);
}
$system_prompts = [
'post' => 'You are a copywriter. Write SEO-optimized blog content.',
'meta' => 'Create SEO meta description under 160 characters.',
'summary' => 'Create brief article summary for featured snippet.',
];
$response = $this->call_anthropic_api(
$system_prompts[$type] ?? $system_prompts['post'],
$prompt
);
return new \WP_REST_Response(['content' => $response]);
}
private function call_anthropic_api(string $system, string $user_message): string {
$response = wp_remote_post('https://api.anthropic.com/v1/messages', [
'headers' => [
'x-api-key' => $this->api_key,
'anthropic-version' => '2023-06-01',
'content-type' => 'application/json',
],
'body' => json_encode([
'model' => 'claude-haiku-4-5',
'max_tokens' => 1024,
'system' => $system,
'messages' => [['role' => 'user', 'content' => $user_message]],
]),
'timeout' => 30,
]);
if (is_wp_error($response)) {
throw new \RuntimeException($response->get_error_message());
}
$body = json_decode(wp_remote_retrieve_body($response), true);
return $body['content'][0]['text'] ?? '';
}
}
new AIAssistant();
Practical Case: Content Agency
Problem: agency publishes 30+ articles per week. Copywriters spent 30–40 min on SEO metadata and structure per article.
Solution: "AI Assistant" button in Gutenberg for generating structure, draft, meta title/description.
Result: article prep time: -45%. SEO metadata: 100% generated by AI, edited manually.
Timeline
- Basic plugin with REST API: 3–5 days
- Gutenberg block: 3–5 days
- Chat widget for visitors: 3–5 days
- WooCommerce integration (product descriptions): +1 week







