Building with the WordPress AI connectors: wp_ai_client_prompt()
Every interaction starts the same way. wp_ai_client_prompt() returns a fluent builder; you chain configuration methods, then call a generation method:
$text = wp_ai_client_prompt( 'Write a haiku about WordPress.' )
->generate_text();
if ( is_wp_error( $text ) ) {
return;
}
echo wp_kses_post( $text );Code language: PHP (php)
Notice the error handling. Generation methods return WP_Error on failure, following WordPress convention, and failure is a normal state here, not an edge case. The site might have no provider at all.
The builder surface covers the usual knobs: using_temperature(), using_max_tokens(), using_system_instruction(), top-p/top-k, stop sequences, and conversation history via with_history(). Beyond plain text, generate_texts( 4 ) returns variations, generate_image() returns a file DTO, and as_json_response( $schema ) constrains the model to a JSON schema for structured output.
Because your plugin cannot know which providers a given site has configured, model selection works by preference, not demand:
$result = wp_ai_client_prompt( 'Summarize the history of the printing press.' )
->using_temperature( 0.1 )
->using_model_preference( 'claude-sonnet-4-6', 'gemini-3.1-pro-preview', 'gpt-5.4' )
->generate_text_result();Code language: PHP (php)
The AI Client walks the list and uses the first available model, falling back to any compatible one if none match. Treat the preference as a hint and write code that works without it. The generate_*_result() variants return a full GenerativeAiResult object with token usage and provider/model metadata, handy for logging costs, and it serializes straight into rest_ensure_response() if you are exposing the feature over the REST API.
Finally, feature detection. Support checks make no API calls and cost nothing, so gate your UI with them:
$probe = wp_ai_client_prompt( 'test' );
if ( $probe->is_supported_for_text_generation() ) {
}Code language: PHP (php)
The full API surface is documented in the official AI Client dev note and the Connectors API dev note on Make/Core. Now for something runnable.