Filter wp_sms_api_message_content

Updated on Oct 25, 2023

This filter hook allows you to modify the content of a message before it is sent via the WordPress SMS API. You can customize the message text based on specific conditions or add additional content to the message body.

Parameters

  1. $message (string) – The original message content.
  2. $args (array) – An array of parameters passed to the filter. It contains request parameters provided when calling the SMS API.

Example Usage

/**
 * Filter to modify message
 */
$message = apply_filters('wp_sms_api_message_content', $message, $request->get_params());

Example Callback Function

add_filter('wp_sms_api_message_content', function ($message, $args) {
    // Check if group_ids parameter is set in the request
    if (isset($args['group_ids']) && $args['group_ids']) {
        // Append group-specific unsubscribe instructions to the message
        $message = $message . "\nTo unsubscribe, reply STOP-{$args['group_ids']} to this SMS.";
    } else {
        // Append general unsubscribe instructions to the message
        $message = $message . "\nTo unsubscribe, reply STOP to this SMS.";
    }

    // You can add more conditions and modify the message further if needed

    return $message;
});

Additional Examples

Example 1: Adding a Timestamp to the Message

add_filter('wp_sms_api_message_content', function ($message, $args) {
    // Add current timestamp to the message
    $message = $message . "\nSent at: " . date('Y-m-d H:i:s');

    return $message;
});

Example 2: Conditional Message Modification

add_filter('wp_sms_api_message_content', function ($message, $args) {
    // Check if a specific keyword is present in the message
    if (strpos($message, 'SPECIALOFFER') !== false) {
        // Modify message for special offers
        $message = "🎉 Special Offer Alert: " . $message;
    }

    return $message;
});

Notes

  • You can create multiple filter functions for wp_sms_api_message_content, and they will be executed in the order they are added.
  • Ensure that the filter function returns the modified message at the end.