wp_ai_client_prompt()WP 7.0.0

Creates a prompt builder object for AI and allows you to describe an AI request through a chain of methods: prompt text, system instruction, preferred models, files, response format, and other parameters.

The function is the main entry point in the AI Client API. It does not directly contact a specific provider: the site itself uses configured AI connectors and selects an appropriate model based on the request’s capabilities.

Through the obtained WP_AI_Client_Prompt_Builder{} you can run text, image, and other result generation, and also check in advance whether the required capability is supported on the current site.

Calling the function itself only creates the object. The AI request is executed later—when the generation method is called, for example generate_text() or generate_image_result().

AI functions may be unavailable even if WordPress supports AI Client. The site must have an appropriate AI connector configured, and the selected provider must support the required type of generation.

No Hooks.

Returns

WP_AI_Client_Prompt_Builder.

  • WP_AI_Client_Prompt_Builder — the prompt builder object through which the AI request is configured and executed.

Usage

wp_ai_client_prompt( $prompt ): WP_AI_Client_Prompt_Builder;
$prompt(string|array|null)

Initial prompt content. Usually a string with the request text is passed.

If the parameter is not provided, the text can be added later via the method WP_AI_Client_Prompt_Builder::with_text().

Default: null

Examples

0

#1 Generating Plain Text

Let’s create a prompt, get a text response, and check the result for errors.

$text = wp_ai_client_prompt( 'Briefly describe the benefits of caching in WordPress.' )
	->using_temperature( 0.7 )
	->generate_text();

if ( is_wp_error( $text ) ) {
	wp_die( esc_html( $text->get_error_message() ) );
}

echo wp_kses_post( $text );
0

#2 Creating a Prompt via Builder Methods

The same request can be assembled gradually. This is convenient when part of the data comes from a form, a post, or plugin settings.

$builder = wp_ai_client_prompt()
	->with_text( 'Write a short description for a post about WordPress security.' )
	->using_system_instruction( 'Answer in simple language. Do not use a promotional style.' );

$text = $builder->generate_text();

if ( is_wp_error( $text ) ) {
	return;
}

echo wp_kses_post( $text );
0

#3 Checking image generation support

Before displaying the image generation interface, it’s better to check whether the site has a suitable AI connector.

$is_supported = wp_ai_client_prompt()
	->with_text( 'test' )
	->is_supported_for_image_generation();

if ( ! $is_supported ) {
	return;
}

// Here you can connect the image generation interface.
0

#4 AI Client image generation

The example creates a prompt builder for image generation and receives the full result object.

use WordPress\AiClient\Files\Enums\FileTypeEnum;
use WordPress\AiClient\Files\Enums\MediaOrientationEnum;

$builder = wp_ai_client_prompt()
	->with_text( 'Generate a clean illustration of a WordPress dashboard on a laptop.' )
	->as_output_file_type( FileTypeEnum::inline() )
	->as_output_media_orientation( MediaOrientationEnum::from( 'landscape' ) );

$result = $builder->generate_image_result();

if ( is_wp_error( $result ) ) {
	wp_die( esc_html( $result->get_error_message() ) );
}

return $result;

Changelog

Since 7.0.0 Introduced.

wp_ai_client_prompt() code WP 7.0.2

function wp_ai_client_prompt( $prompt = null ): WP_AI_Client_Prompt_Builder {
	return new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), $prompt );
}