How to Build a WordPress AI Plugin (Step-by-Step Guide 2026)

Published: (June 11, 2026 at 04:45 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

How to Build a WordPress AI Plugin (Step-by-Step Guide 2026)

This guide builds a complete WordPress plugin with AI content generation: settings page, OpenAI API wrapper, secure AJAX handlers, and a Gutenberg sidebar panel. wp-ai-assistant/ ├── wp-ai-assistant.php # Headers + init ├── includes/class-api.php # OpenAI wrapper ├── includes/class-ajax.php# AJAX handlers └── assets/editor.js # Gutenberg sidebar

class WP_AI_Assistant_API { public function generate(string $prompt): string { $client = OpenAI::client(get_option(‘wp_ai_assistant_api_key’)); $response = $client->chat()->create([ ‘model’ => ‘gpt-4o-mini’, ‘messages’ => [[‘role’ => ‘user’, ‘content’ => $prompt]], ]); return $response->choices[0]->message->content; } }

add_action(‘wp_ajax_wp_ai_generate’, function () { check_ajax_referer(‘wp_ai_assistant_nonce’, ‘nonce’); if (!current_user_can(‘edit_posts’)) wp_send_json_error(‘Forbidden’, 403); $prompt = sanitize_textarea_field($_POST[‘prompt’] ?? ”); try { $result = (new WP_AI_Assistant_API())->generate($prompt); wp_send_json_success([‘content’ => $result]); } catch (Exception $e) { wp_send_json_error($e->getMessage()); } });

const { registerPlugin } = wp.plugins; registerPlugin(‘wp-ai-assistant’, { render: AIAssistantPanel }); // Panel calls admin-ajax.php → inserts generated content as a paragraph block

check_ajax_referer() on every AJAX action current_user_can() before processing sanitize_* on all inputs, esc_* on all outputs Store API key in wp_options, never in code Originally published at kalyna.pro

0 views
Back to Blog

Related posts

Read more »

The spec is in the wrong place

My day job is at a large tech company. Hundreds of engineering teams, and every one of them is somewhere different on AI adoption. Some are still treating codin...

The Heuristics Say Don't

A culture that only records its disasters ends up with a biased archive. Wars documented, plagues chronicled, collapses catalogued. The quiet decades go unwritt...