Filter
uncanny-automator
automator_whatsapp_default_type_message_return
Filters the default message type for WhatsApp messages, allowing customization before it's sent.
add_filter( 'automator_whatsapp_default_type_message_return', $callback, 10, 2 );
Description
Filters the default WhatsApp message string when an unrecognized message type is encountered. Developers can modify the default output, typically a string like "(type) message_id", to provide custom default messages or log additional details before the message is processed.
Usage
add_filter( 'automator_whatsapp_default_type_message_return', 'your_function_name', 10, 2 );
Parameters
-
$default(mixed) - This parameter holds the default message type, which is 'text' if not explicitly specified.
-
$message(mixed) - This parameter represents the default message type that will be used if no specific type is detected or provided.
Return Value
The filtered value.
Examples
add_filter( 'automator_whatsapp_default_type_message_return', 'my_custom_whatsapp_message_return', 10, 2 );
/**
* Customizes the default message return for WhatsApp messages.
*
* This function intercepts the default return value for WhatsApp messages
* that are not of a specific type (like 'button'). It allows developers
* to modify how these default messages are displayed, perhaps by adding
* custom prefixes or suffixes, or by logging more information.
*
* @param string $default The default message string generated by the plugin.
* @param array $message The entire message array, including type and ID.
* @return string The modified message string to be returned.
*/
function my_custom_whatsapp_message_return( $default, $message ) {
// Example: Add a custom prefix to all default WhatsApp messages.
// This could be useful for distinguishing automated messages in logs
// or for user-facing notifications.
$custom_prefix = '[Automated WhatsApp] ';
// Ensure $message and $message['id'] are valid before using them.
if ( isset( $message['id'] ) && is_string( $message['id'] ) ) {
// You could also choose to log additional information here for debugging.
// error_log( "Processing default WhatsApp message with ID: " . $message['id'] );
// Return the prefixed default message.
return $custom_prefix . $default;
} else {
// If the message structure is unexpected, return the original default
// to avoid errors, or handle it as per your application's needs.
return $custom_prefix . ' [Invalid Message Data]';
}
}
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:310
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 );
}
}