Search posts

Search posts

Rich Tabor

Design. Engineering. Product.

How to Use the WordPress AI Client

The upcoming release of WordPress 7.0 ships with the new WP AI Client. You will be able to can call any AI model (Anthropic, Google, OpenAI, others) through a single API, in PHP directly and from JavaScript using the Abilities API.

Black and white engraving-style illustration of a spiral galaxy with tiny human figures standing on rocky terrain below, gazing upward.

WP AI Client is infrastructure.

It doesn’t ship any providers or make any AI calls by default. It’s only the plumbing that your plugin can build on. If you don’t want to use AI with your website, it’s all good.

Let’s look at what it is, then build something with it.

Why core

Today, any AI-powered WordPress plugin ships its own API wrapper, own settings page for credentials, and own provider logic. If you install three plugins that use AI, you’ll end up configuring three separate API keys for the same provider. And if said provider changes their API for any reason, all three plugins break independently.

The WP AI Client fixes this the same way WordPress fixed database access with $wpdb or HTTP requests with wp_remote_post(), with one shared layer.

Configure once, use everywhere.

How it works

The WP AI Client is a provider-agnostic API for calling generative AI models.

It sits on top of the PHP AI Client, adapted for WordPress conventions: WP HTTP transport, WP_Error handling, provider credential discovery from environment variables/constants, and an Abilities/REST-backed JavaScript path for browser contexts.

To try it out today, you’ll need WordPress 7.0 (currently in beta) and at least one provider.

There are currently providers for Anthropic, Google, and OpenAI are on WordPress.org, with others already showing up, like Grok and OpenRouter. Install one and define your API key (e.g. ANTHROPIC_API_KEY) to run abilities.

It’s heavy and manual in the current beta, but there’s an admin page in the works that will make it easier to add and configure providers.

The plugins we write never touch credentials or provider logic. We just write the prompts and functions while the client handles the rest, like this:

In PHP:

$text = wp_ai_client_prompt( 'Summarize this post.' )
    ->using_system_instruction( 'You write concise summaries. Return plain text only.' )
    ->generate_text();

In JavaScript (via abilities):

import '@wordpress/core-abilities';
import { executeAbility } from '@wordpress/abilities';

const result = await executeAbility(
    'ai-content-helpers/generate-excerpt',
    { post_id: 123 }
);

We can also check whether a provider is configured before rendering an AI feature, so plugins degrade gracefully:

if ( function_exists( 'wp_ai_client_prompt' ) ) {
    // Show the AI feature.
} else {
    // Show setup instructions.
}

And notably, REST/JavaScript execution permissions are controlled per ability via each ability’s permission_callback.

Now let’s see this in practice.

Example: Auto-generating alt text

I built a companion plugin to this post called WP AI Content Kit to serve as an example. It’s a working plugin that you can install, test, look at the code, and learn more, without overcomplicating things.

This first example is a feature that auto-generates alternative text for images as you upload them, or manually for older media.

The using_system_instruction() method sets the role and rules, the prompt is the actual task, and with_file() attaches the image so the model can see it.

$image_file = get_attached_file( $attachment_id );
$mime_type  = get_post_mime_type( $attachment_id );

$alt_text = wp_ai_client_prompt( 'Generate alt text for this image.' )
    ->using_system_instruction(
        'You are an accessibility expert that generates alt text for website images. '
        . 'Keep it under 125 characters. Describe visible content objectively. '
        . 'Do not start with "Image of" or "Photo of". Return plain text only.'
    )
    ->with_file( $image_file, $mime_type )
    ->using_model_preference( 'claude-sonnet-4-6', 'gpt-5.1' )
    ->generate_text();

The using_model_preference() method sets preferred models in priority order, falling back to automatic selection if none are available.

Example: Auto-generating excerpts

Today WordPress will take the first 50 or so words from a post and call it an excerpt. But what if excerpts actually summarized the post? In this example I’m showing how to leverage the as_json_response() method, which tells the model to return structured JSON matching a schema I’ve defined.

This way you get a predictable object instead of free-form text you’d have to parse:

$content = wp_strip_all_tags( $post->post_content );

$schema = array(
    'type'       => 'object',
    'properties' => array(
        'excerpt' => array(
            'type'        => 'string',
            'description' => 'A 1-2 sentence summary of the post.',
        ),
    ),
    'required'   => array( 'excerpt' ),
);

$json = wp_ai_client_prompt( "Write an excerpt for this post:\n\n" . $content )
    ->using_system_instruction(
        'You generate post excerpts for WordPress. '
        . 'Write 1-2 sentences. Be specific about what the content covers. '
        . 'Preserve the source tone. Avoid generic filler and cliches.'
    )
    ->as_json_response( $schema )
    ->generate_text();

Going deeper

Do check out WP AI Content Kit on GitHub, and if you want to see these APIs used at a bigger scale, the AI Experiments plugin goes deeper with more abilities.

WordPress 7.0 Beta 1 is out, and there’s no better time to start building AI into your plugins. The full release is scheduled for April 9, coinciding with WordCamp Asia (I’ll be there!).

So what will you build with it?