PHP code example of wordpress / wp-ai-client

1. Go to this page and download the library: Download wordpress/wp-ai-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wordpress / wp-ai-client example snippets


add_action( 'init', array( 'WordPress\AI_Client\AI_Client', 'init' ) );

add_action(
	'admin_enqueue_scripts',
	static function () {
		wp_enqueue_script( 'wp-ai-client' );
	}
);

$text = wp_ai_client_prompt( 'Write a haiku about WordPress.' )
	->generate_text();

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

echo wp_kses_post( $text );

$image_file = wp_ai_client_prompt( 'A futuristic WordPress logo in neon style' )
	->generate_image();

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

$data_uri = $image_file->getDataUri();

echo '<img src="' . esc_url( $data_uri ) . '" alt="A futuristic WordPress logo in neon style">';

$schema = array(
	'type'       => 'array',
	'items'      => array(
		'type'       => 'object',
		'properties' => array(
			'plugin_name' => array( 'type' => 'string' ),
			'category'    => array( 'type' => 'string' ),
		),
		'_text();

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

// Output will be a JSON string adhering to the schema.
$data = json_decode( $json, true );

$images = wp_ai_client_prompt( 'Aerial shot of snowy plains, cinematic.' )
	->generate_images( 4 );

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

foreach ( $images as $image_file ) {
	echo '<img src="' . esc_url( $image_file->getDataUri() ) . '" alt="Aerial shot of snowy plains">';
}

use WordPress\AiClient\Messages\Enums\ModalityEnum;

$result = wp_ai_client_prompt( 'Create a recipe for a chocolate cake and 
	wp_die( $result->get_error_message() );
}

// Iterate through the message parts.
foreach ( $result->toMessage()->getParts() as $part ) {
	if ( $part->isText() ) {
		echo wp_kses_post( $part->getText() );
	} elseif ( $part->isFile() && $part->getFile()->isImage() ) {
		echo '<img src="' . esc_url( $part->getFile()->getDataUri() ) . '" alt="">';
	}
}

$summary = wp_ai_client_prompt( 'Summarize the history of the printing press.' )
	->using_temperature( 0.1 )
	->using_model_preference(
		'claude-sonnet-4-5',
		'gemini-3-pro-preview',
		'gpt-5.1'
	)
	->generate_text();

use WordPress\AI_Client\AI_Client;

$text = AI_Client::prompt( 'Explain quantum computing in complicated terms.' )
	->using_max_tokens( 8000 )
	->generate_text();

use WordPress\AiClient\ProviderImplementations\Anthropic\AnthropicProvider as Anthropic;

$text = wp_ai_client_prompt( 'Explain quantum computing in simple terms.' )
	->using_model( Anthropic::model( 'claude-sonnet-4-5' ) )
	->generate_text();

$prompt = wp_ai_client_prompt( 'Explain quantum computing in simple terms.' )
	->using_temperature( 0.2 );

if ( $prompt->is_supported_for_text_generation() ) {
	// Safe to generate.
	$text = $prompt->generate_text();
} else {
	// Fallback: Hide feature or show setup instructions.
}

$text = wp_ai_client_prompt( 'Hello' )
	->generate_text();

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

echo wp_kses_post( $text );

use WordPress\AI_Client\AI_Client;

try {
	$text = AI_Client::prompt( 'Hello' )->generate_text();
} catch ( \Exception $e ) {
	wp_die( $e->getMessage() );
}

echo wp_kses_post( $text );