A book by Alok Jain
AI in WordPress
A Practical, Code-First Guide
WordPress 7.0 introduced a built-in AI Client — one consistent way to work with models from Anthropic, Google, and OpenAI.
This free, chapter-by-chapter, code-first guide helps you understand how it works through practical examples. You’ll explore temperature, tokens, structured output, multimodal responses, and more.
Each example is added to the same companion plugin as you progress, leaving you with one working reference instead of a folder of disconnected code snippets.

Table of contents
Read at your own pace
Module 1 — Foundations
Goal: first working call, safely.
- What Is the AI Client? 1 Provider-agnostic API, Connectors, why it matters
- Your First AI Call 2 Prompt → generate → result
- Never Trust the Network — Error Handling 3 WP_Error convention, the habit to build now
- Debugging AI Client Failures 4 is_wp_error() tells you that something failed; this is how you find out why
- Giving the AI a Job — System Instructions 5 Role vs. request
Module 2 — Controlling the Output
Goal: precise, predictable, cost-aware text generation.
- The Creativity Dial — Temperature 6 Randomness vs. determinism
- Don’t Blow the Budget — Max Tokens 7 What a token is; cost & length control
- Ask for JSON, Not Prose 8 Structured output via JSON schema
- Using the Data, Saving JSON to Post Meta 9 Decoding & using the data (save to post meta)
- Give Me Options — Multiple Variations 10 Several candidates, one call
- Fine-Tuning Generation, Top-P, Top-K, and Stop Sequences 11 Alternative sampling controls, plain-English, when to reach for these vs. temperature
Module 3 — Beyond Text
Goal: images and mixed-media output.
Module 4 — Giving the AI Context
Goal: move beyond single-shot prompts.
Module 5 — Production-Ready Habits
Goal: code that belongs in a real, published plugin.
- Don’t Build UI Blindly, Feature Detection 17 Check before you render; graceful degradation
- What Actually Happened? — Full Result Objects 18 Metadata: tokens, provider, model
- Politely Requesting a Model — Model Preferences 19 Preference vs. requirement; fallback chain
- Exposing AI to the Frontend, Safely 20 Why there's no direct client-side call; building your own scoped REST endpoint
- Who Gets to Use AI? — Access Control 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 22 Registering a static block whose editor UI calls a new REST endpoint (same pattern as Ep. 20) via apiFetch
- Add “Improve with AI” to Existing Blocks 23 Extending core blocks via block filters/toolbar controls, modifying RichText content
- AI Image Block — Generate & Insert Images from the Editor 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 25 Public-facing REST endpoint security: nonces, rate limiting, capability scoping for logged-out users
- Smart Comment Reply Suggestions 26 Logged-in-only frontend feature, JS + REST
- A Multi-Turn Chatbot Widget on the Frontend 27 Combining with_history() (Ep. 16) with the REST pattern (Ep. 20) for a stateful frontend chat
Module 8 — Capstone Projects
Goal: combine everything into shippable features.
- Capstone 1: AI Alt-Text Generator 28 Prompt + system instruction + JSON schema + error handling + feature detection + REST endpoint — admin/Media Library feature
- Capstone 2: AI Content Block + Frontend Widget 29 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