Filter
uncanny-automator
automator_whatsapp_button_text
Filters the text displayed on the WhatsApp share button, allowing customization before it appears to users.
add_filter( 'automator_whatsapp_button_text', $callback, 10, 2 );
Description
Filters the text displayed on a WhatsApp button. Developers can modify the button text before it's sent in a message. This hook is used when generating content for WhatsApp messages, specifically for button elements.
Usage
add_filter( 'automator_whatsapp_button_text', 'your_function_name', 10, 2 );
Parameters
-
$button_text(mixed) - This parameter holds the current text content of the WhatsApp button, which can be filtered and modified.
-
$message(mixed) - This parameter holds the text that will be displayed on the WhatsApp button.
Return Value
The filtered value.
Examples
add_filter( 'automator_whatsapp_button_text', 'my_custom_whatsapp_button_text', 10, 2 );
/**
* Changes the text of a WhatsApp button dynamically.
*
* This function modifies the default button text for a WhatsApp message.
* It can be used to prepend or append text, or even change the text completely
* based on the message content or other conditions.
*
* @param string $button_text The original button text.
* @param array $message The complete message array, which might contain details to influence the button text.
* @return string The modified button text.
*/
function my_custom_whatsapp_button_text( $button_text, $message ) {
// Example: If the message contains a specific keyword, change the button text.
if ( isset( $message['payload'] ) && strpos( $message['payload'], 'urgent' ) !== false ) {
$button_text = 'Contact Support Now!';
} elseif ( ! empty( $button_text ) ) {
// Example: Append a general call to action if button text exists.
$button_text .= ' (Learn More)';
} else {
// If no button text is provided by default, set a default.
$button_text = 'Click Here';
}
return $button_text;
}
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:306
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 );
}
}