Chapter 2
Your First AI Call
Prompt → generate → result
Chapter 1 ended with this, and nothing else:
$builder = wp_ai_client_prompt();
We built something but never used it. Let’s actually generate text with it, and give it a proper home to live in while we’re at it.
Setting up a companion plugin
Every example in this course goes into one plugin. You’ll keep adding to it as we go, one file per chapter, so by the end you have a single, working reference for everything covered in the series.
The main plugin file stays simple: a header, an ABSPATH guard, and a loop that loads whatever’s inside an includes folder. Nothing else. Set that up now inside wp-content/plugins/:
ai-client-course-examples/
├── ai-client-course-examples.php
└── includes/
ai-client-course-examples.php is the bootstrap. It only needs to be written once, adding a new chapter later never means editing this file again:
Go to Plugins and activate it. It doesn’t do anything yet, the includes folder is empty. Let’s give it something to load.
Generating your first haiku
Inside includes, create chapter-02-your-first-ai-call.php:
Save it. The bootstrap’s glob() loop picks up any file dropped into includes automatically, so there’s nothing else to wire up. Create a new page, add the shortcode [ai_course_ch2] anywhere on it (a Shortcode block, or just typed into a Paragraph block, both work), publish, and open the page.
You should see an actual haiku on screen, written by whichever AI model your site has connected. That’s the whole loop in five lines: a prompt goes in, generate_text() sends it off, and a finished piece of text comes back.
What happens when generate_text() runs
(Quick note before the AI part: the function returns instead of echos because that’s what a shortcode callback requires, and $text goes through wp_kses_post() because it’s still just an external string that needs sanitizing before it hits the page. Nothing new for anyone who’s built a shortcode before, just flagging it since we’ll keep writing shortcodes this way for the rest of the series.)
The part that’s actually new here: everything before generate_text() is just configuration. No request leaves your server until that method is called. The moment it is, WordPress sends your prompt to the connected provider and waits for a reply, and that reply comes back as a plain string, nothing more.
This is also where a small, real cost gets charged to whatever API key is connected. Not something to worry about for one haiku, but worth remembering once you’re calling this on every page load instead of once, on demand.
Two ways to write the same prompt
We passed the prompt straight into wp_ai_client_prompt() above, as its first argument. There’s also a with_text() method that does the exact same thing:
$text = wp_ai_client_prompt()
->with_text( 'Write a haiku about WordPress.' )
->generate_text();
Both lines produce identical output. with_text() isn’t doing anything different behind the scenes, it’s the same call written differently.
The reason it exists: right now, with nothing else configured on the builder, passing the prompt directly is just less to type, so that’s what Chapter 2’s example uses. But starting next chapter we’ll be chaining more onto this builder, a system instruction, then a temperature setting, then more after that. Once you’re chaining several using_*() calls together, having the prompt sit in its own with_text() line makes the whole chain read as one consistent list of configuration, instead of one line that looks different from everything below it. Both forms keep working the whole series through. Which one you reach for depends on how much else you’re chaining onto the same builder.
Why this function will break, on purpose
Try disconnecting your AI provider for a second (or just wait for a rate limit, it happens) and reload the page. The whole page breaks, not just the shortcode. That’s because ai_course_ch2_first_call() assumes generate_text() always succeeds, and right now, it doesn’t check.
That’s deliberate for this one example, so the core pattern stays visible without extra code cluttering it. It’s also exactly what Chapter 3 exists to fix, and every function we write after that one checks for failure before doing anything else with the result.
Try it yourself
Change the prompt inside ai_course_ch2_first_call() to something you’d actually use. A couple of starting points:
'Write a one-sentence tagline for a bakery website.''Summarize the plot of Romeo and Juliet in two sentences.'- Whatever plugin or client project you’re already working on right now
Save, reload the page, and see what comes back. Then reload it again without touching anything. The wording won’t be identical the second time. That’s not a bug, it’s exactly what Chapter 5, on temperature, is about.