Skip to content
WP EngineDocumentation

Using the WordPress AI Client

If you’re building a WordPress plugin or theme, you may not need to call the AI API directly. WordPress 7.0 ships a built-in AI Client with a fluent PHP API for text, image, and speech generation, and the WP Engine AI Connector plugin registers a wpengine provider for it — so your code never touches an API key, and auth, retries, and provider routing are handled for you.

  1. Install and activate the WP Engine AI Connector plugin.
  2. From Settings → WP Engine AI Connector, connect the site to a AI console project.
  3. In your plugin or theme, depend on the WordPress AI Client (bundled with WordPress 7.0+; install via Composer on earlier versions).

Generate text with the fluent builder. The SDK discovers a suitable model from the registered providers, sends the request through the connector, and returns the candidate text.

<?php
declare( strict_types = 1 );
use WordPress\AiClient\AiClient;
$summary = AiClient::prompt( 'Summarise the speed of light in one sentence.' )
->usingSystemInstruction( 'You are concise.' )
->usingTemperature( 0.2 )
->generateText();

generateText() returns a plain string. For multiple candidates or the full result object (token usage, finish reason, etc.) use generateTexts() or generateTextResult() respectively.

Pin a specific model when you need one for cost, latency, or capability reasons — the SDK falls through the list until it finds one the connector exposes:

$summary = AiClient::prompt( 'Summarise the speed of light in one sentence.' )
->usingProvider( 'wpengine' )
->usingModelPreference( 'google/gemini-3.5-flash' )
->generateText();

The connector reports itself as unavailable when the site isn’t connected. Gate user-facing features on that signal so a disconnected site degrades gracefully instead of throwing:

if ( ! AiClient::isConfigured( 'wpengine' ) ) {
// Show an admin notice or fall back to a non-AI code path.
return;
}

Swap generateText() for generateImage() to produce an image instead. The SDK returns a File object — handle it as a stream and write it through WordPress’s media APIs if you intend to persist it.

use WordPress\AiClient\AiClient;
use WordPress\AiClient\Files\DTO\File;
$image = AiClient::prompt( 'A pixel-art lighthouse at sunset' )
->usingProvider( 'wpengine' )
->generateImage();
// $image is a WordPress\AiClient\Files\DTO\File — persist or stream it.

The AI Client is a provider-agnostic content-generation layer, not a full AI API client. It’s built to work against any registered AI provider, so it only exposes generation primitives generic enough to make sense across providers — it has no method surface for:

  • Token counting, transcription, image alt-text, content summarization, or taxonomy suggestion
  • Usage analytics or credit balance
  • Site registration or connection management (handled entirely by the AI Connector plugin, not the SDK)
  • Agents, sessions, or knowledge base collections
  • The MCP transport used by external clients like Claude Code

For any of those, call the AI API directly — see the API reference for every operation.

Last updated: