A book by Alok Jain
AI in WordPress
A Practical, Code-First Guide
WordPress 7.0 shipped a built-in AI Client, one function that works the same whether you’re calling Claude, Gemini, or GPT. This is a chapter-by-chapter, code-first guide to actually understanding it. Temperature, tokens, structured output, multimodal responses, each one gets a small example you run yourself, added to the same companion plugin file as you go, so you end up with one working reference instead of a folder of disconnected snippets.

Table of contents
Read at your own pace
Module 1 — Foundations
Goal: first working call, safely.
Module 2 — Controlling the Output
Goal: precise, predictable, cost-aware text generation.
- The Creativity Dial — Temperature 5 Randomness vs. determinism
- Don’t Blow the Budget — Max Tokens Draft 6 What a token is; cost & length control
- Ask for JSON, Not Prose (Part A) Draft 7 Structured output via JSON schema
- Ask for JSON, Not Prose (Part B) Draft 8 Decoding & using the data (save to post meta)
- Give Me Options — Multiple Variations Draft 9 Several candidates, one call
- Fine-Tuning Generation — Top-P, Top-K & Stop Sequences Draft 10 Alternative sampling controls, plain-English, when to reach for these vs. temperature
Module 3 — Beyond Text
Goal: images and mixed-media output.
- Your First AI Image Draft 11 Same pattern, new modality
- Image Variations & Output Control Draft 12 Multiple images, file type, orientation
- Text and Images Together — Multimodal Output Draft 13 Mixed-part responses
Module 4 — Giving the AI Context
Goal: move beyond single-shot prompts.
- Give the AI a File Draft 14 Passing an image/document as input (e.g. auto-describe an uploaded image)
- Multi-Turn Conversations Draft 16 Why LLMs are stateless and how history fixes that; build a mini Q&A assistant
Module 5 — Production-Ready Habits
Goal: code that belongs in a real, published plugin.
- Don’t Build UI Blindly — Feature Detection Draft 17 Check before you render; graceful degradation
- What Actually Happened? — Full Result Objects Draft 18 Metadata: tokens, provider, model
- Politely Requesting a Model — Model Preferences Draft 19 Preference vs. requirement; fallback chain
- Exposing AI to the Frontend, Safely Draft 20 Why there's no direct client-side call; building your own scoped REST endpoint
- Who Gets to Use AI? — Access Control Draft 21 Blocking/limiting prompts globally or conditionally
Module 6 — AI Inside the Block Editor (Gutenberg)
Goal: bring AI into the actual authoring experience, not just admin-side PHP.
- Build Your First AI-Powered Block Draft 22 Registering a dynamic block whose editor UI calls your Ep. 18 REST endpoint via apiFetch
- Add “Improve with AI” to Existing Blocks Draft 23 Extending core blocks via block filters/toolbar controls, modifying RichText content
- AI Image Block — Generate & Insert Images from the Editor Draft 24 Editor-side image generation UI, inserting into Media Library / Image block
Module 7 — Frontend-Facing AI Features
Goal: AI features your site's visitors actually use — not just wp-admin.
- A Public AI-Powered FAQ / Search Widget Draft 25 Public-facing REST endpoint security: nonces, rate limiting, capability scoping for logged-out users
- Smart Comment Reply Suggestions Draft 26 Logged-in-only frontend feature, JS + REST
- A Multi-Turn Chatbot Widget on the Frontend Draft 27 Combining with_history() (Ep. 15) with the REST pattern (Ep. 19) for a stateful frontend chat
Module 8 — Under the Hood (short, optional/bonus)
Goal: context for developers who want to go deeper or maintain existing plugins.
- How the AI Client Is Actually Built Draft 28 Two-layer architecture: PHP AI Client SDK vs. WP Core wrapper; why snake_case + WP_Error on top of camelCase + exceptions; credential management via Connectors API; the three official provider plugins
- Migrating Existing AI Plugins to WordPress 7.0 Draft 29 For devs with existing php-ai-client/wp-ai-client code: Requires at least: 7.0, replacing AI_Client::prompt(), conditional autoloading for backward compatibility
Module 9 — Capstone Projects
Goal: combine everything into shippable features.
- Capstone 1: AI Alt-Text Generator Draft 30 Prompt + system instruction + JSON schema + error handling + feature detection + REST endpoint — admin/Media Library feature
- Capstone 2: AI Content Block + Frontend Widget Draft 31 Custom block (Module 6) + public REST + history (Module 7) — a block editor tool and the matching frontend-facing experience it powers
This book is created with Chapterwright