Send SMS from WordPress
WordPress ships wp_remote_get, so a few lines in a small plugin or in your theme's functions.php are enough. The examples below send a text when a WooCommerce order comes in and when someone fills in a form.
Credentials and the helper
// wp-config.php
define('BUDGETSMS_USERNAME', 'USERNAME');
define('BUDGETSMS_USERID', 'USERID');
define('BUDGETSMS_HANDLE', 'HANDLE');
define('BUDGETSMS_FROM', 'MyShop');
<?php
/*
Plugin Name: BudgetSMS notifications
Description: Sends SMS through the BudgetSMS HTTP API.
*/
/** Sends an SMS; returns the message id, or a WP_Error with the API error code. */
function budgetsms_send(string $to, string $message, string $from = BUDGETSMS_FROM) {
$url = add_query_arg([
'username' => BUDGETSMS_USERNAME, 'userid' => BUDGETSMS_USERID, 'handle' => BUDGETSMS_HANDLE,
'from' => $from, 'to' => $to, 'msg' => $message,
], 'https://api.budgetsms.net/sendsms/');
$response = wp_remote_get($url, ['timeout' => 20]);
if (is_wp_error($response)) {
return $response;
}
$body = trim(wp_remote_retrieve_body($response));
if (strpos($body, 'OK') !== 0) {
return new WP_Error('budgetsms', $body); // "ERR 1001" etc., see the error code list
}
return (int) strtok(substr($body, 3), ' ');
}
Use testsms instead of sendsms in the URL while testing: same parameters and answer format, but it is a simulator: no SMS is sent and nothing is charged, the message id and price in the answer are random, and about one in five calls returns a random ERR 2xxx so your error handling gets exercised (the destination number is not checked).
Text yourself on every WooCommerce order
add_action('woocommerce_thankyou', function (int $order_id) {
$order = wc_get_order($order_id);
if ($order) {
budgetsms_send('31612345678', 'New order #' . $order->get_order_number() . ' of ' . $order->get_formatted_order_total() . ' by ' . $order->get_billing_first_name());
}
});
Confirm an order to the customer
Customer numbers come in all shapes; strip everything but digits and make sure the country code is in front (here the Netherlands, 31):
function budgetsms_international(string $phone, string $country_code = '31'): string {
$digits = preg_replace('/\D/', '', $phone);
if (strpos($digits, '00') === 0) {
$digits = substr($digits, 2);
} elseif (strpos($digits, '0') === 0) {
$digits = $country_code . substr($digits, 1);
}
return $digits;
}
add_action('woocommerce_order_status_completed', function (int $order_id) {
$order = wc_get_order($order_id);
if ($order && $order->get_billing_phone()) {
budgetsms_send(budgetsms_international($order->get_billing_phone()), 'Your order #' . $order->get_order_number() . ' has been shipped. Thank you!');
}
});
Delivery reports
If you want to know whether a message arrived, register a Push DLR URL in the control panel and handle it with a small REST route:
add_action('rest_api_init', function () {
register_rest_route('budgetsms/v1', '/dlr', [
'methods' => 'GET',
'permission_callback' => '__return_true',
'callback' => function (WP_REST_Request $request) {
// $request['id'] = message id, $request['status'] 1 = delivered, 2 and 3 = failed, $request['date'] = unix timestamp
update_option('budgetsms_last_dlr', ['id' => (int) $request['id'], 'status' => (int) $request['status'], 'date' => (int) $request['date']]);
return 'OK';
},
]);
});
// Push DLR URL: https://example.com/wp-json/budgetsms/v1/dlr