Filter
uncanny-automator
automator_openai_image_generate_description
Filters the prompt used to generate images via OpenAI before it's sent to the API.
add_filter( 'automator_openai_image_generate_description', $callback, 10, 1 );
Description
Filters the image description used when saving OpenAI-generated images to the WordPress media library. Developers can modify the `$prompt` parameter to customize the alt text, caption, or description associated with the image. This hook fires after the image URL is generated but before it's saved, allowing for dynamic description generation.
Usage
add_filter( 'automator_openai_image_generate_description', 'your_function_name', 10, 1 );
Parameters
-
$prompt(mixed) - This parameter holds the text prompt used to generate the image, which can be modified by the filter.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the description for an image generated by OpenAI.
* This function will append a specific suffix to the original prompt,
* making it more descriptive for accessibility and SEO purposes.
*
* @param string $prompt The original prompt used to generate the image.
* @return string The modified prompt to be used as the image description.
*/
add_filter(
'automator_openai_image_generate_description',
function ( $prompt ) {
// Ensure the prompt is a string before appending.
if ( ! is_string( $prompt ) ) {
$prompt = (string) $prompt; // Cast to string if it's not already.
}
// Append a descriptive suffix to the original prompt.
$modified_prompt = $prompt . ' - Generated by AI';
return $modified_prompt;
},
10, // Priority
1 // Accepted arguments
);
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/open-ai/actions/open-ai-image-generate-dall-e.php:238
public function insert_to_media( $image_url = '', $prompt = '' ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$description = apply_filters( 'automator_openai_image_generate_description', $prompt );
$attachment_id = media_sideload_image( $image_url, null, $description, 'id' );
if ( is_wp_error( $attachment_id ) ) {
throw new Exception( esc_html( $attachment_id->get_error_message() ), 500 );
}
return $attachment_id;
}