Skip to content

Instantly share code, notes, and snippets.

@soderlind
Created May 13, 2026 08:27
Show Gist options
  • Select an option

  • Save soderlind/754a2ceb28a69d1a2e3dd556a2c63374 to your computer and use it in GitHub Desktop.

Select an option

Save soderlind/754a2ceb28a69d1a2e3dd556a2c63374 to your computer and use it in GitHub Desktop.
How to use wp_ai_client_prompt( $prompt )

Below are complete WordPress-style examples that extend your snippets with basic WP_Error handling, sanitization, escaping, and flexible handling for common return shapes.

I’m assuming:

  • wp_ai_client_prompt( $prompt ) returns a prompt object.
  • generate_text() returns a string or WP_Error.
  • generate_image() returns an image URL, attachment ID, array like ['url' => ...], or WP_Error.
  • convert_text_to_speech() returns an audio URL, attachment ID, array like ['url' => ...], or WP_Error.
<?php
/**
 * Text generation.
 */
function my_ai_text_example(): void {
	$response = wp_ai_client_prompt( 'Explain gravity in one sentence.' )->generate_text();

	if ( is_wp_error( $response ) ) {
		echo '<p>' . esc_html( $response->get_error_message() ) . '</p>';
		return;
	}

	echo '<p>' . esc_html( $response ) . '</p>';
}

my_ai_text_example();
<?php
/**
 * Image generation.
 */
function my_ai_image_example(): void {
	$image = wp_ai_client_prompt( 'A tiny blue cat on a cloud' )->generate_image();

	if ( is_wp_error( $image ) ) {
		echo '<p>' . esc_html( $image->get_error_message() ) . '</p>';
		return;
	}

	$image_url = '';

	if ( is_numeric( $image ) ) {
		$image_url = wp_get_attachment_image_url( (int) $image, 'large' );
	} elseif ( is_array( $image ) && ! empty( $image['url'] ) ) {
		$image_url = $image['url'];
	} elseif ( is_string( $image ) ) {
		$image_url = $image;
	}

	if ( empty( $image_url ) ) {
		echo '<p>' . esc_html__( 'No image was generated.', 'my-textdomain' ) . '</p>';
		return;
	}

	printf(
		'<img src="%s" alt="%s" loading="lazy" />',
		esc_url( $image_url ),
		esc_attr__( 'A tiny blue cat on a cloud', 'my-textdomain' )
	);
}

my_ai_image_example();
<?php
/**
 * Text-to-speech.
 */
function my_ai_text_to_speech_example(): void {
	$audio = wp_ai_client_prompt( 'Hello world' )->convert_text_to_speech();

	if ( is_wp_error( $audio ) ) {
		echo '<p>' . esc_html( $audio->get_error_message() ) . '</p>';
		return;
	}

	$audio_url = '';

	if ( is_numeric( $audio ) ) {
		$audio_url = wp_get_attachment_url( (int) $audio );
	} elseif ( is_array( $audio ) && ! empty( $audio['url'] ) ) {
		$audio_url = $audio['url'];
	} elseif ( is_string( $audio ) ) {
		$audio_url = $audio;
	}

	if ( empty( $audio_url ) ) {
		echo '<p>' . esc_html__( 'No audio was generated.', 'my-textdomain' ) . '</p>';
		return;
	}

	printf(
		'<audio controls src="%s">%s</audio>',
		esc_url( $audio_url ),
		esc_html__( 'Your browser does not support the audio element.', 'my-textdomain' )
	);
}

my_ai_text_to_speech_example();

And here’s a shortcode version for user-provided prompts:

<?php
add_shortcode( 'ai_text', 'my_ai_text_shortcode' );

function my_ai_text_shortcode( array $atts ): string {
	$atts = shortcode_atts(
		[
			'prompt' => 'Explain gravity in one sentence.',
		],
		$atts,
		'ai_text'
	);

	$prompt = sanitize_text_field( $atts['prompt'] );

	if ( '' === $prompt ) {
		return esc_html__( 'Please provide a prompt.', 'my-textdomain' );
	}

	$response = wp_ai_client_prompt( $prompt )->generate_text();

	if ( is_wp_error( $response ) ) {
		return esc_html( $response->get_error_message() );
	}

	return '<p>' . esc_html( $response ) . '</p>';
}

Usage:

[ai_text prompt="Explain gravity in one sentence."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment