Skip to content

Module 1 — Foundations

Goal: first working call, safely.

Most WordPress plugins that add AI features today do it the hard way. You install a provider’s SDK, store an API key in a settings page, write your own wrapper around the HTTP calls, and hope the response format doesn’t change in the next SDK release. If a client wants to switch from OpenAI to Claude next year, you’re rewriting that integration.

WordPress 7.0 adds a built-in AI Client to core that gets rid of most of this. This module covers the building blocks you need before touching anything else in this book: what the AI Client actually is, how to make your first call, how to handle it when that call fails, how to actually see what went wrong when it does, and how to give the AI a proper role instead of just a question.

We’re spending a full module on this instead of rushing to the “fun” stuff (images, custom blocks, chatbots) for a reason. Every later chapter reuses the exact same pattern: wp_ai_client_prompt(), chain some configuration, call a generation method, just with more options added on top. If this part feels shaky, everything after it will too.

What’s in this module

  1. What Is the AI Client? What “provider-agnostic” means in practice, and where the site owner actually configures their provider. (You’ll never see their API key, and that’s the point.)
  2. Your First AI Call. The smallest working example, generating real text on screen.
  3. Never Trust the Network, Error Handling. AI calls go through an external API over HTTP, so they fail sometimes, same as any other remote request. We build the habit of checking for that from the very first example, not after the first bug report.
  4. Debugging AI Client Failures. A friendly error message tells you that something failed. This is how you find out why, before you’re stuck guessing.
  5. Giving the AI a Job, System Instructions. The difference between a generic answer and one written by something that already knows what role it’s playing.

One honest note before we start: none of this requires you to understand how LLMs work internally. You don’t need to know what a transformer is. You need to know how to send a prompt, read the result, and handle the cases where it doesn’t come back clean. That’s the whole module.

Let’s start with what the AI Client actually is.

This book is created with Chapterwright