Skip to book content
Back to library

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 2 — Controlling the Output

Goal: precise, predictable, cost-aware text generation.

  1. The Creativity Dial — Temperature Randomness vs. determinism
  2. Don’t Blow the Budget — Max Tokens Draft What a token is; cost & length control
  3. Ask for JSON, Not Prose (Part A) Draft Structured output via JSON schema
  4. Ask for JSON, Not Prose (Part B) Draft Decoding & using the data (save to post meta)
  5. Give Me Options — Multiple Variations Draft Several candidates, one call
  6. Fine-Tuning Generation — Top-P, Top-K & Stop Sequences Draft Alternative sampling controls, plain-English, when to reach for these vs. temperature

Module 3 — Beyond Text

Goal: images and mixed-media output.

  1. Your First AI Image Draft Same pattern, new modality
  2. Image Variations & Output Control Draft Multiple images, file type, orientation
  3. Text and Images Together — Multimodal Output Draft Mixed-part responses

Module 4 — Giving the AI Context

Goal: move beyond single-shot prompts.

  1. Give the AI a File Draft Passing an image/document as input (e.g. auto-describe an uploaded image)
  2. Multi-Turn Conversations Draft 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.

  1. Don’t Build UI Blindly — Feature Detection Draft Check before you render; graceful degradation
  2. What Actually Happened? — Full Result Objects Draft Metadata: tokens, provider, model
  3. Politely Requesting a Model — Model Preferences Draft Preference vs. requirement; fallback chain
  4. Exposing AI to the Frontend, Safely Draft Why there's no direct client-side call; building your own scoped REST endpoint
  5. Who Gets to Use AI? — Access Control Draft 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.

  1. Build Your First AI-Powered Block Draft Registering a dynamic block whose editor UI calls your Ep. 18 REST endpoint via apiFetch
  2. Add “Improve with AI” to Existing Blocks Draft Extending core blocks via block filters/toolbar controls, modifying RichText content
  3. AI Image Block — Generate & Insert Images from the Editor Draft 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.

  1. A Public AI-Powered FAQ / Search Widget Draft Public-facing REST endpoint security: nonces, rate limiting, capability scoping for logged-out users
  2. Smart Comment Reply Suggestions Draft Logged-in-only frontend feature, JS + REST
  3. A Multi-Turn Chatbot Widget on the Frontend Draft 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.

  1. How the AI Client Is Actually Built Draft 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
  2. Migrating Existing AI Plugins to WordPress 7.0 Draft 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.

  1. Capstone 1: AI Alt-Text Generator Draft Prompt + system instruction + JSON schema + error handling + feature detection + REST endpoint — admin/Media Library feature
  2. Capstone 2: AI Content Block + Frontend Widget Draft 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