Filter
uncanny-automator
automator_whatsapp_image_caption
Filters the image caption for WhatsApp messages, allowing modification before sending.
add_filter( 'automator_whatsapp_image_caption', $callback, 10, 2 );
Description
Filters the WhatsApp image ID and caption before it's added to the message. Developers can use this hook to modify the image ID and caption string, allowing for custom formatting or the inclusion/exclusion of specific information. The hook fires after the image ID and caption are retrieved from the message but before they are returned.
Usage
add_filter( 'automator_whatsapp_image_caption', 'your_function_name', 10, 2 );
Parameters
-
$image_id_caption(mixed) - This parameter contains the caption associated with an image message, if one exists.
-
$message(mixed) - This parameter holds the caption associated with an image message, which can be of mixed types.
Return Value
The filtered value.
Examples
/**
* Filter the WhatsApp image caption to potentially modify it.
*
* This example adds a prefix to the caption if it's empty, indicating
* that it's an image without a user-provided caption.
*
* @param string $image_id_caption The original image ID and caption string.
* @param array $message The full WhatsApp message array.
* @return string The potentially modified image caption string.
*/
add_filter( 'automator_whatsapp_image_caption', function ( $image_id_caption, $message ) {
// Extract caption and image ID for easier manipulation.
$caption = isset( $message['image']['caption'] ) ? $message['image']['caption'] : '';
$image_id = isset( $message['image']['id'] ) ? $message['image']['id'] : '';
// If the caption is empty but an image ID exists, provide a default indicator.
if ( empty( $caption ) && ! empty( $image_id ) ) {
$image_id_caption = sprintf( '(Image ID: %1$s) No Caption Provided', $image_id );
} elseif ( ! empty( $caption ) ) {
// If there is a caption, prepend the image ID to it for clarity.
$image_id_caption = sprintf( '(%1$s) %2$s', $image_id, $caption );
} else {
// Fallback for unexpected scenarios.
$image_id_caption = sprintf( 'Image Message (ID: %1$s)', $message['id'] ?? 'N/A' );
}
return $image_id_caption;
}, 10, 2 );
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/whatsapp/helpers/whatsapp-helpers.php:302
protected function extract_message( $message ) {
$type = isset( $message['type'] ) ? $message['type'] : 'text';
switch ( $type ) {
case 'text':
// Return the text body.
return isset( $message['text']['body'] ) ? $message['text']['body'] : '';
case 'image':
$caption = isset( $message['image']['caption'] ) ? $message['image']['caption'] : '';
$image_id = isset( $message['image']['id'] ) ? $message['image']['id'] : '';
$image_id_caption = sprintf( '(%1$s) %2$s', $image_id, $caption );
// Return the image id + caption.
return apply_filters( 'automator_whatsapp_image_caption', $image_id_caption, $message );
case 'button':
$button_text = isset( $message['button']['text'] ) ? $message['button']['text'] : '';
// Return the button text.
return apply_filters( 'automator_whatsapp_button_text', $button_text, $message );
default:
// Otherwise, just return the type and message ID for now.
$default = sprintf( '(%s) %s', $type, $message['id'] );
return apply_filters( 'automator_whatsapp_default_type_message_return', $default, $message );
}
}